views:

1084

answers:

4

Hey guys,

I'm wanting to learn about creating windows services with C#. I've googled around a bit and I've found a few tutorials. I was wondering if there are any frameworks that I could use that would help with designing the services. I've searched various online book stores for books on the subject; however I've come up short.

If you could please recommend some of the following:

  • books dedicated on creating and debugging windows services
  • SOA design patterns for C#

any recommendations would be greatly appreciated!

please note that i am not developing web applications, these services will be installed as components and distributed across servers.

Thanks!

+7  A: 

When you say "Windows Services" I assume you are talking about background Windows programs in the services.msc snap in. You mention SOA though, so are you talking about a distributed service using Component Services?

Windows services are pretty easy. Start with the a Windows Service project in Visual Studio, which gives you the framework. Then fill in the missing bits. The help files also give some good samples.

For debugging, I use the following so that I can run it as a console app as well:

static void Main(string[] args)
{
    if (Environment.UserInteractive)
    {
        try
        {
            Console.WriteLine(Properties.Resources.Starting);
            // Interactive, run as a console app
            MyService controller = new MyService();
            controller.OnStart(args);

            Console.WriteLine(Properties.Resources.Started);
            Console.Read();

            Console.WriteLine(Properties.Resources.Stopping);
            controller.OnStop();
            Console.WriteLine(Properties.Resources.Stopped);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine();
            Console.WriteLine(ex.StackTrace);
        }
        Console.Read();
    }
    else
    {
        // Non-interactive, run as a windows service
        ServiceBase.Run(new MyService());
    }
}
Robert Wagner
As an alternative you can also call upon System.Diagnostics.Debugger.Break(); this will allow you to attach a debugger to the service at it's creation.
olle
@olle That is definitely an alternative, this however allows debugging right out of VS
Robert Wagner
Thank you Robert; Not exactly what i was looking for; but it great information none-the-less!
Michael G
A: 

Just create a new project in Visual Studio (or Visual Studio Express) and select "Windows Service" as the project type. This will give you the basic OnStart, OnStop framework you need. Your class will be setup to inherit from ServiceBase with overriden OnStart and OnStop methods ready for you to fill in.

scottm
+1  A: 

The WCF framework brings Web Services, Remoting and Tcp based services under the same roof. If you are building distributed services this is almost certainly the place to start.

I found the WCF chapter in this book a really good intro. Clear, well written and informative. This is the more in depth WCF book to check out.

flesh
+5  A: 

(As it so happens I literally just finished a windows service on a project of mine.)

I used a framework that is "in development" but so far has worked well. It's called Topshelf (n.b. it depends on StructureMap & log4net).

It handles all the messy stuff with Windows Services, such as dependencies, credentials and so on. Basically you create a configuration, sort of like this (more examples in the link):

var cfg = RunnerConfigurator.New(x =>
{
   x.SetDisplayName("Timeout Service");
   x.SetServiceName("mt_timeout");

   x.ConfigureService<TestService>(c=>
       {
           c.WhenStarted(s => s.Start());
           c.WhenStopped(s => s.Stop());
           c.WhenPaused(s => { });
           c.WhenContinued(s => { });
       });

   x.DoNotStartAutomatically();

   // Multiple options available here, including prompt
   x.RunAsLocalSystem();

   x.DependsOn("ServiceName");
});

Runner.Host(cfg, args)

Apart from doing a lot of stuff for you, it has a two distinct advantages:

  • the installer is built in, so you can just run your "app.exe /install" and it will install (without using the .NET utility InstallService.exe - part of the exe).
  • you can run the app as a console app for debugging. This is a huge advantage over other methods such as Thread.Sleep at the beginning of your service to give enough time for debugger to attach (or options like this). The latter may cause you some headaches when you install the service, debug the app, dettach the debugger and try to uninstall the service. In some cases (when the app was left running, some rogue thread or so) the service will be marked for deletion, meaning you can't install a new version with the same name, but you can't remove the old one without a reboot.

(A last word: I cannot compare this two any other win service framework, because frankly so far I did it myself... kind of "not-invented-here" syndrome :| )

Hope that helps

AlexDuggleby