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!
