domingo, 23 de agosto de 2009

Good Practices for Windows Services

This time I'd like to give some general guidelines you might like to take in count when creating a Windows Service Project.

Let's talk about distribution.  I think a good distribution and order can make your life easier.  When talking about windows service I like to have all the logic separate in a Class Library Project.  This will make my life easy and you will know why further.

So when I create a Windows Service Project I also create a Class Library that contains all the logic. My Windows Service Project then will have a class (a Thread usually so I start and stop when neccesary) that will perform the action (what my windows service will do) by synchronizing and coordinating the access to the multiple classes and services in my Class Library Project.

I also add a Console Project for debugging and testing purposes.  The main method will perfom exactly the same as the OnStart method in the windows service.  I just need to copy our main class from the windows service and change the namespace and that's it.

When I start coding I usually need to throw messages, exceptions, etc.  I like to have a shared interface in my shared Code Library project ILogger.  ILogger will have the definition of only one method: void WriteEntry(string message); .

This way Windows Service Project will have a class that implements this interface and will use the EventLog while my Console Project will have a class that will write the entry using System.Console.  All the clases in the Code Library Project will use the ILogger interface to log an message or a exception.  I think this is called DependencyInjection and it's useful!

So at the end, when writing a windows service you'll end up with at least 4 projects in the solution if you add a Setup Project for installation.  Having this distribution will make your life easier than putting everything in your Windows Service I swear.

Hope this will be helpful for someone!

Start a Windows Service after Installation

Recently I had to deal with this problem: How to start (automatically) a windows service after installing it?

It took me a while to find the answer, so I decided to post this to make it easier for anyone in the same situation.

First, How do I install a windows service? Piece of cake, you have several options: write a script, programatically, using a deployment project.  I personally like the last option.  This is a step by step guide of how to create setup project for you windows service.

Next step, go to the code view of the Project Installer class and in constructor add a new handler like this:

this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall);

Now add this function:

private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
  {
  ServiceController controller = new ServiceController("ServiceName");
  try
  {
  controller.Start();
  }
  catch (Exception ex)
  {
  String source = "MyService Installer";
  String log = "Application";
  if (!EventLog.SourceExists(source))
  {
  EventLog.CreateEventSource(source, log);
  }

  EventLog eLog = new EventLog();
  eLog.Source = source;

  eLog.WriteEntry(@"The service could not be started. Please start the service manually. Error: " + ex.Message, EventLogEntryType.Error);

  }
  }


And that's it.  Check the Event Viewer and if you get an error check the service name parameter when creating the ServiceController.

Don't forget to add these Namespaces: System.ServiceProcess; System.Diagnostics;

I won't take credit for this, this code is part of a set of tutorials related to windows services I found in Grinn Blog. Thanks Grinn!

If you decide to use a script anyway (instead of doing this programatically) you just need to insert this command (net start [service]) after the installing instruction.

sábado, 15 de agosto de 2009

SL 3 + RIA Services (Getting Started)

So, this is getting started with Silverlight 3 and RIA Services. I'm not going to develop an application or give a deep explanation on this topic. There's already a lot of this in the web.

My intention here is to provide you the links I think are the most valuables to start developing and this way save you a lot of searching time.

What is RIA Services basically about? I'd say three things: Entities, Services and UI (Silverlight).

This is a great tutorial that covers these three subjects. This is the first one you should read since it's very clean, understandable and covers the basic. Link

This tutorial from Brad Adams goes a little bit further. You'll be able to play with styles, data validation and authentication as well. Definetly the next step. Link

In case you want to go deeper on Entities and how they relate to each other (one-to-many, many-to-many, etc). I couldn't find a better explanation on this topic. Link

There you are. You are now ready to develop you first Silverlight 3 application with RIA Services.