I've been lucky enough to attend an Architects Master Class presented by Juval Lowy from IDesign this past week, and one point briefly covered was that, for the foreseeable future, self-hosting WCF services inside a Windows service was going to be the way to go. Very briefly: Although Windows 2008/WAS seems to solve this problem, you won't be able to use WAS for the service bus functionality in C# 4.0, so it looks like its back to, or still in my case, Windows services.
That being said, a gripe that often crops up with developers about Windows services in general is how difficult they are to debug. The majority of these developers solve the problem by creating a console application that does the hosting while they're developing/debugging, and C&P the code over to the service when it's time to deploy.
The sample project I've included with this post is actually my service project template. In the main function, all I do is check for a command line parameter, and then decide whether to start running as a console app, or to hand off to the usual code that starts the service.
Expand Code
static void Main(string[] args)
{
#if DEBUG
if (args.Length >= 1)
{
if (args[0].Equals("/debug", StringComparison.CurrentCultureIgnoreCase))
{
DebugMain(args);
return;
}
}
#endif
ServiceMain(args);
}
#if DEBUG
/// <summary>
/// Will be called to start the "service" if the command line is /debug
/// </summary>
private static void DebugMain (string[] args)
{
Console.WriteLine("Starting service...");
using (var service = new MyService())
{
service.DebugStart(args);
Console.WriteLine("Service Started.");
Console.WriteLine("\r\nPress <ENTER> to exit...");
Console.ReadLine();
}
}
#endif
/// <summary>
/// Will be called to start when it needs to be a service
/// </summary>
private static void ServiceMain(string[] args)
{
var ServicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(ServicesToRun);
}
}
Also included in my project is a ready-to-use service installer class. You can add the project as a custom action assembly to a Windows Installer project, and the service will be installed by the MSI.
Hope this helps!
Service.Host.zip