Friday, October 8, 2010

Wcf self hosting - also in VS 2010

@YaronNaveh

A while ago I published a Wcf self hosting template project for visual studio 2008.
The same template also works for VS 2010: just put this under

"%My Documents%\Visual Studio 2008\Templates\ProjectTemplates\Visual C#\WCF"

and the new template is under the C# / Wcf node:



Enjoy :)

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

2 comments:

Olivier said...

Thanks Yaron. Always useful posts!
What I found out to be very useful in case of self hosting is the capability to run the same code either as a console app or as a windows service. This allows for example to be able to debug without having to install the windows service and attach visual studio for debugging.
For that, some code is needed in the Main() of the ServiceBase class:
#if (!DEBUG)
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new AdminConsoleWindowsService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
// Debug code: this allows the process to run as a non-service.
// It will kick off the service start point, but never kill it.
// Shut down the debugger to exit
AdminConsoleWindowsService service = new AdminConsoleWindowsService();
service.StartWCFService();
// Put a breakpoint on the following line to always catch
// your service when it has finished its work
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif

This is from Lee Humphries: http://www.codeproject.com/KB/dotnet/DebugWinServices.aspx?fid=172874&df=90&mpp=10&noise=4&sort=Position&view=Expanded&fr=11

Cheers,
Olivier.

Yaron Naveh (MVP) said...

Olivier

This is a great idea. Usually I would create a service library and then have a separate executable to host it, but this is better.