Sunday, 15 March 2009

AttachFile Plug-in

attachfile

I’m preparing a release to CodePlex and the Windows Live Gallery of my Windows Live Writer plug-in Attach File. The battery on my laptop is about to run out though so I honestly can’t be bothered with the MSI file that is a requirement of the Gallery submission. So, at the bottom of this post is a direct link to a zip containing the DLLs needed to add the plug-in functionality to Live Writer. Simply drag the two files to the Plugins folder of the Windows Live Writer installation directory.

I should note that the second of the two files is a third party DLL from CodePlex that provides the FTP communications layer. I will eventually do my own, but credit where it is due. The FTP project can be located at http://ftpclient.codeplex.com/.

Also, the icon is from FamFamFam. And yes, the link below was created using AttachFile.

AttachFile.zip

Attach To Process

Just a quick note on debugging Windows Live Writer plug-ins. I only mention it because I have not seen it discussed elsewhere, you can attach Visual Studio to the Live Writer process. Attaching to the Live Writer process enables you to step through the code line by line, as you would a Windows Forms App in Visual Studio.

If you are already developing Live Writer plug-ins, you may be aware that by adding the following line to the Post Build Event, you can import the plug-in directly into Live Writer upon completion of a successful build.

post build

XCOPY /D /Y /R "$(TargetPath)" "C:\Program Files\Windows Live\Writer\Plugins\"

On loading up Live Writer after successful build, switch back to Visual Studio and click on Debug > Attach to Process. You’ll now be presented with a window similar to the one in the screenshot below.

Attach

Double clicking on WindowsLiveWriter.exe will now provide debugging capabilities to the plug-in. Try it, create a a couple of break points and start interacting with the plug-in in Live Writer. I use this same technique for debugging Windows Services.

Wednesday, 4 March 2009

Setting Up a Windows Service Project in VS2008

Setting up, debugging and deploying a windows service can be a daunting task, but it is actually relatively straight forward. Relative to something ever so slightly more difficult to a console application.

Before I jump into the steps I have one piece of advice that has helped me side step a lot of grief. It is possible to debug a windows service using Visual Studio, by installing it on your development machine. The process can become a bit laborious though, in the early stages of development. My advice is to write all the guts of your service in a separate class library and add that library (as a reference) to a console application in the same solution. Use the console application to debug the majority of your code before adding the windows service project. You can then debug the various service state changes with the windows service itself.

Also, I have noticed that running a windows service on Vista is not exactly the same as running a service on Windows Server 2003. I don’t always do it, but I recommend some sort of internal logging so that you can work out any quirks without the aide of Visual Studio. I have my own code, that I am preparing to add to my K3R library, but I notice Live Labs have there own library on CodePlex now. The Live Labs logging library can be found here.

  1. Add a windows service project. Add references to your class libraries, if you have them.
  2. Open up Service1.cs (I’ve renamed mine to Service.cs) in design mode. This can be achieved by right clicking on Service.cs and selecting “View Designer”.
  3. Right click anywhere on the grey area of Service.cs and select “Add Installer”. This creates a new file called ProjectInstaller.cs.
  4. In the design mode of Project Installer.cs, I renamed serviceProcessInstaller1 to ServiceProcessInstaller and selected the account I would the service to run under. This is all done by selecting what was serviceProcessInstaller1 and opening the Properties window.
  5. I also renamed serviceInstaller1 to ServiceInstaller and personalised a few of the Misc properties; Description, Service Name, etc… The important property here though, is StartType. The selections you make here should be self explanatory.
  6. Add a setup project to the solution. I called mine ServiceSetup.
  7. Right click on the ServiceSetup project and click Add > Project Output. Select the windows service project from the drop down and add the project’s “Primary output” to setup project.
  8. Right click on the Service Setup project again and click View > Custom Actions. Right click on the root of Custom Actions and click Add Custom Action. Select the “Primary output” (of the Service project) from the Application Folder.
  9. Set up the properties of the new setup project, getting briefly annoyed that there isn’t a British locale.
  10. You are there. Providing your solution builds, you’re ready to install your service on a test machine. Remeber, you will have to start the service manually first of all, even if you have set the StartType to Automatic.

I should say, this post really constitutes a quick start guide and if you’re looking for more detailed information, I recommend an excellent series of posts on Arcane Code, links of which I’ve listed below.

  1. Getting Started
  2. Getting Started
  3. Adding the Installer
  4. Debugging Windows Services
  5. Controlling Your Service from Visual Studio
  6. Controlling Your Service from Another Application
  7. Sending Commands to your Windows Service
  8. Pulling in the Event Log for your Windows Service

Tuesday, 3 March 2009

Operator, can you connect me to 555 Coolsville?

I’ve admired the operator keyword from afar for some time, the ability to overload operators and conversions is pretty wicked. It wasn’t until this morning that I had a requirement, so I’ve had a little play, in order to understand the basics.

My first experiment was very basic, my intention was to create a class that I can add together using the + operator. The code is below.

public class Account {
 public int Balance { get; set; }

 public static int operator + (Account a, Account b) {
   return a.Balance + b.Balance;
 }
}

var a = new Account { Balance = 5 };
var b = new Account { Balance = 5 };

int totalBalance = a + b; // 10

Now I happen to think that is pretty smart. My example was very basic, but think of all of the calculations that could be encapsulated within an operator overload. Within this framework you could calculate multiple property values and even output to a secondary custom class representing a balance sheet.

This last example was created to understand the relationship between calculations involving more than two instances of a class. Before I continue, I realise there are already short cuts in place to quickly create Generic lists. This example overloads the % operator to allow the quick creation of Generic lists. What is particularly interesting is that you can describe the relationships between your class and any other class that it may encounter.

public class Account {
 public int Balance { get; set; };

 public static List operator %(Account a, Account b) {
  return new List {
   a,
   b
  };
 }

 public static List operator %(List a, Account b) {
  a.Add(b);
  return a;
 }
}

var a = new Account { Balance = 5 };
var b = new Account { Balance = 5 };
var c = new Account { Balance = 6 };

(a % b % c).Count; // 3

Imagine the example above returning a custom collection or another object that allowed you to query the three accounts simultaneously for balances and other financial totals.

Twitter Updates