Skip to content

Self Host ASP.NET Web Api as a Windows Service

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.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


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. service 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

TopshelfThere 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 Topshelf
Next 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. Self Host ASP.NET Web API To verify my web api application is responding, here is the result from my Postman call Postman  

2-hour Video Course for $30 USD

Sign up for more info!

* indicates required
 

Source Code

GithubThe source code for this demo is available on Github.

Next…

Next up I’ll continue adding to the demo by serving static assets from your self host application.

7 thoughts on “Self Host ASP.NET Web Api as a Windows Service”

  1. Pingback: How-To Create Katana Middleware - CodeOpinion

  2. Pingback: ASP.NET Self Host Static File Server - CodeOpinion

  3. Pingback: 2016 | Pearltrees

  4. Pingback: Background Tasks in .NET - CodeOpinion

  5. 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

  6. 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
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *