Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Host Web API as Windows Service using OWIN

I'm trying to run a Web API application as a Windows Service using OWIN. However, I get the following message, when trying to start the service:

The [ServiceName] service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.

For some reason my service doesn't understand that it should keep listening to http://localhost:9000

The VS solution consists of two projects: The Web API and the Windows service.

In the Windows service project I have:

static class Program
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service() 
        };
        ServiceBase.Run(ServicesToRun);
    }
}

public partial class Service : ServiceBase
{
    private const string _baseAddress = "http://localhost:9000/";
    private IDisposable _server = null;

    public Service()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        _server = WebApp.Start<Startup>(url: _baseAddress);
    }

    protected override void OnStop()
    {
        if (_server != null)
        {
            _server.Dispose();
        }
        base.OnStop();
    }
}

In OnStart the Startup refers to the Startup class in the Web API project:

public class Startup
{
    public void Configuration(IAppBuilder builder)
    {
        var config = new HttpConfiguration();        
        config.Routes.MapHttpRoute("Default",
            "{controller}/{id}",
            new { id = RouteParameter.Optional });
        builder.UseWebApi(config);
    }
}

(It also contains some configurations for Unity and logging.)

I guess the installer part of the Windows service project is mostly irrelevant, though it could be useful to know that I have:

this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;

What I've tried:

  • To host the Web API using a console application and then host the console application as a Windows Service. But even though I included 'Console.ReadKey()', it simply stopped.
  • To follow this guide: OWIN-WebAPI-Service The weird thing is that I can get his service to work, but when I tried changing my code to match his set-up, I kept getting the same error.

Full source code: github.com/SabrinaMH/RoundTheClock-Backend/tree/exploring_hosting

like image 286
SabrinaMH Avatar asked Feb 15 '15 14:02

SabrinaMH


People also ask

Can Web API be hosted in Windows Service?

You can host a Web API as separate process than ASP.NET. It means you can host a Web API in console application or windows service or OWIN or any other process that is managed by . NET framework.

How do I host a web service in Windows service?

Install and run the serviceOpen Developer Command Prompt for Visual Studio and navigate to the project directory. Type installutil bin\service.exe at the command prompt to install the Windows service. Type services. msc at the command prompt to access the Service Control Manager (SCM).

What is Microsoft OWIN used for in Web API?

OWIN allows web apps to be decoupled from web servers. It defines a standard way for middleware to be used in a pipeline to handle requests and associated responses. ASP.NET Core applications and middleware can interoperate with OWIN-based applications, servers, and middleware.

Can we host OWIN in IIS?

Hosting of OWIN in IISUse the following procedure. Step 1: Open the Visual Studio and create the "New Project". Step 2: Create the new ASP.NET Web Application and enter the name for the application. Step 3: Select the Empty Project Template to create the application.


1 Answers

When you are getting 'service on Local Computer started and then stopped', generally means there's uncaught exception while starting the service. Take a look at Windows service on Local Computer started and then stopped error, for tips to look for that exception.

Based on what you described, my guess the issue is caused by the Startup class exists on a different project, have you tried to have the startup class within the window service project?

Also, the link from HStackOverflow (https://github.com/danesparza/OWIN-WebAPI-Service), shows a work-around approach to load controllers from different project, or dynamically resolve assembly into the current AppDomain. I guess that's also worth trying.

Hope this helps.

like image 93
wlizar Avatar answered Oct 14 '22 02:10

wlizar