Saturday, May 30, 2009

Debuggable Self-Host Windows Service Projects

When developing self-hosted WCF services, you really need two things:

  1. A windows service project that will be the host for the deployed environment.
  2. A console self-host project for easy debugging while developing the service.

Yes, there is also the option to use a WCF Service Library project with its associated WcfServiceHost.exe process that self hosts the service for debugging. However, I have run into too many occasions where something is going wrong and not having direct access to the hosting code wastes more cycles than it takes to set up my own service host, so other than quick demos, I never use the WcfServiceHost.exe in production development.

Having two separate projects to address 1 & 2 above is not really that great either, but if you try to run a service project in the debugger, you get an error telling you it can only be run through the services panel in Windows.

However, through a quick and easy pattern, you can make your service projects so they run as a console for development, but run as a service just fine when installed.

Step 1: Create a Windows Service project for the self host environment.

This is just the standard selection in the project dialog for a Windows Service project. Once you have your service class, rename it as appropriate and right click on the designer surface to add an installer class for the service. Standard stuff here.

Step 2: Implement your hosting code in a separate class from the service class itself.

For example, here is a very simple hosting class that can be used for any service in any project (with some appropriate exception handling and logging added in for production purposes):

public class SelfHost<T>
{
    ServiceHost m_Host;
    public void Start()
    {
        m_Host = new ServiceHost(typeof(T));
        m_Host.Open();
    }

    public void Stop()
    {
        m_Host.Close();
    }
}

From the service class itself, you can just instantiate an this class and call its Start and Stop methods.

public partial class SimpleServiceHostService : ServiceBase
{
    SelfHost<SomeService> m_Host = new SelfHost<SomeService>();
    public SimpleServiceHostService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        m_Host.Start();
    }

    protected override void OnStop()
    {
        m_Host.Stop();
    }
}

Now when you install this service, the service host will be started and stopped through the services panel or automatically depending on the service configuration.

Step 3: Modify the Main method to start conditionally as a Console or a service.

Modify the default main declaration with no parameter to the signature for a main that takes a string[] of arguments. If arguments are present, run the code as if you are in a console app. If no arguments, call the default code for starting it as a service. For the console host mode, just call the Start and Stop methods on an instance of your host class just like the windows service did.

static void Main(string[] args)
{
    // Run as console if there are command line arguments
    if (args.Length > 0)
    {
        SelfHost<SomeService> host = new SelfHost<SomeService>();
        host.Start();
        Console.WriteLine("Press Enter to Exit...");
        Console.ReadLine();
        host.Stop();
    }
    else // run as service
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new SimpleServiceHostService() 
        };
        ServiceBase.Run(ServicesToRun);
    }
}

 

Step 4: Modify the project properties to be a console application and add a debug command line parameter.

ApplicationType

DebugOptions

That is basically it. Change the code shown above to use your service type, add an app.config with the appropriate WCF service code, and you are ready to run as a console app in the debugger, but when you build and install your service as a service, it will run fine as that as well.

You can download a full sample application here.





Saturday, May 30, 2009 4:35:29 PM (GMT Standard Time, UTC+00:00)
Comments [12]  | 


Sunday, May 31, 2009 10:16:15 PM (GMT Standard Time, UTC+00:00)
Good idea, but why not use the Environment.UserInteractive flag to determine whether to run in console mode?
droyad
Tuesday, June 02, 2009 5:35:00 PM (GMT Standard Time, UTC+00:00)
Thanks. Don't want to rain on your blog post, but the guys building Mass Transit have extracted a really useful library called TopShelf (http://code.google.com/p/topshelf/) that does exactly this (hosting options are: service, console, and winform). It's a really nice library and fairly easy to use. The result is a compiled application that you can install as a service (using command line switches like: 'myapp.exe /install') or just run on the command line as a console app (by just executing the app). You can even install the service as a named install a la SQL Server from the command line (myapp.exe /install /name:instance1)
Jeremy Wiebe
Wednesday, June 03, 2009 7:39:02 AM (GMT Standard Time, UTC+00:00)
very good
[url=http://www.htyytt.com]very beautiful[/url]
It’s very good !
ahjgdg
Wednesday, June 03, 2009 7:40:01 AM (GMT Standard Time, UTC+00:00)
very good
[url=http://www.htyytt.com]very beautiful[/url]
It’s very good !
bvcbg
Wednesday, June 03, 2009 7:41:06 AM (GMT Standard Time, UTC+00:00)
And when im feelin incomplete your my Roll forming machine missing piece. And when you need your
forming machine breath taken away ill be your thief.

Ask not what your Replica Handbags can do for you; ask what you can do for your Replica Rolex.
There is no such thing as darkness wow power leveling, only a failure to see power leveling.I am crasy about wow gold ,meanwhile i am also interested in faye and arvil's songs.
vxcv
Friday, June 05, 2009 5:17:50 AM (GMT Standard Time, UTC+00:00)
Ask not what your Replica Handbags can do for you; ask what you can do for your Replica Rolex. silkroad online gold silkroad gold

Thursday, June 25, 2009 8:17:55 AM (GMT Standard Time, UTC+00:00)
Nice post Fashion site
Jack
Saturday, June 27, 2009 5:13:32 PM (GMT Standard Time, UTC+00:00)
The replica rolex Date is 34mm like the Air-King, but it obviously includes a date function. IIts production began in the late 1950s around the time the steel Datejust was introduced. Retails range from $5,250 with a smooth bezel and Oyster bracelet to $7,000 with an 18K white gold fluted bezel and diamond markers, to $15,050 for 18K Yellow gold on a jubilee bracelet. rolex replica model was once offered in 3 flavors: steel, 18k yellow & steel, and all 18k yellow gold. replica rolex watches ares now only available in steel or solid yellow gold and replica watches
Tuesday, July 07, 2009 8:37:53 AM (GMT Standard Time, UTC+00:00)
Thursday, July 30, 2009 1:19:29 PM (GMT Standard Time, UTC+00:00)
Hey. We thought, because we had power, we had wisdom. Help me! I can not find sites on the: Professional squash association. I found only this - job bank in nc. All news for financial companies of about in financial ambac financial group, Local customized commercial loan programs is a fixed interest rate loan in which commercial real estate serves as collateral. :mad: Thanks in advance. Erasmus from Comoros.
Comments are closed.









Ads by Lake Quincy Media









Sign In
Copyright © 2006-2007 Brian Noyes. All rights reserved.
Advertise on this site through Lake Quincy Media
designed by NUKEATION STUDIOS