Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
In my previous blog I demonstrated how to Self Host ASP.NET Web API, which was a very basic console application leveraging Owin and Katana. The next step in this series is how to turn your console application into a Windows service. If you have any questions, please follow me on Twitter.Video
If you prefer a video tutorial, here is one I published to YouTube outlining a similar example as on this post.Windows Service Template
The typical way to create a Windows Service is by using the Windows Service template when creating a new project within Visual Studio. The template will create a Program.cs calling ServiceBase.Run() in the Main() method. SelfHostServiceBase is the code that will extend ServiceBase. You would have to implement OnStart() and OnStop() That’s simplest possible implementation. Now when you start debugging, you are treated with this wonderful message. What this means is you must build your Windows service, start it, then attach the debugger to the process. Doesn’t sound like a fun way to debug does it?Topself
There is a great open source project called Topshelf which allows you to create a windows service which is much easier to debug.Topshelf is a framework for hosting services written using the .NET framework. The creation of services is simplified, allowing developers to create a simple console application that can be installed as a service using Topshelf. The reason for this is simple: It is far easier to debug a console application than a service. And once the application is tested and ready for production, Topshelf makes it easy to install the application as a service.First thing is to install Topshelf into your project via nuget.
Install-Package TopshelfNext we need a tiny bit of code to configure our service with topshelf. We tell topself which class will contain our service (TopselfService) as well as which method to call when the service starts and stops. Also, you can define which user the service should run as, as well as the name, display name and description. Now when you debug the application, it will run just like a Console application, in which you can add breakpoints and debug. To verify my web api application is responding, here is the result from my Postman call
Pingback: How-To Create Katana Middleware - CodeOpinion
Pingback: ASP.NET Self Host Static File Server - CodeOpinion
Pingback: 2016 | Pearltrees
Pingback: Background Tasks in .NET - CodeOpinion
Hi Derek,
Excellent post!! Also, thanks for the introduction to TopShelf…makes things a lot easier.
I’ve poured over your sample, implemented it myself and everything works great – but what I’d like to do (and failing miserably) is separate out the controller(s) into a separate API project so the self host project contains only the hosting details and my API project can contain one or more controllers. Two questions:
– How can I go about this?
– What are your thoughts on doing this? Personally, I love a clean separation of things and having my controllers lay alongside my hosting infrastructure just feels dirty to me.
Thanks for your time and thanks again for the post!!
-Mark
TopShelf is awesome. Thanks for your post too
Hi,
To run the service in DEBUG you can create a C # Directive to call the start code of your application.
Ex.:
static class Program
{
///
/// Ponto de entrada principal para o aplicativo.
///
static void Main()
{
#if (!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#else
Service1 service = new Service1();
service.Iniciar(); — call here your startup routines
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif
}
}