12

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

xleon
  • 6,201
  • 3
  • 36
  • 52
SabrinaMH
  • 221
  • 1
  • 3
  • 11
  • I'm getting pretty much exactly the same behaviour. Did you find a solution? – ben Mar 26 '15 at 15:07
  • No, unfortunately not. Please let me know if you find a solution. I stumbled upon the same service message in another project, which didn't concern OWIN. There, the solution was to make sure that the config file in the service project contained the same informations as the application project itself (as the Windows service project used the config file in it's own project). But I haven't gotten around to trying that fix on this problem. – SabrinaMH Mar 27 '15 at 17:22
  • I found this post useful: http://stackoverflow.com/questions/4864673/windows-service-to-run-constantly – ben Mar 30 '15 at 16:15
  • Thanks! I'll have a look at it :) – SabrinaMH Mar 31 '15 at 18:18
  • Did you check this link; http://piotrwalat.net/hosting-web-api-in-windows-service/ – Teoman shipahi Jul 01 '15 at 13:35
  • got the same problem here... – Gianluca Ghettini Jul 13 '15 at 15:22
  • Unfortunately, I still haven't found a solution. Please let me know if you do. – SabrinaMH Jul 14 '15 at 17:22
  • Try this: https://github.com/danesparza/OWIN-WebAPI-Service – rubStackOverflow Jul 26 '15 at 20:29

2 Answers2

4

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.

Community
  • 1
  • 1
wlizar
  • 56
  • 2
2

For that example OWIN-WebAPI-Service, you must install Package

Install-Package Microsoft.Owin.Host.HttpListener
pedrofernandes
  • 16,354
  • 10
  • 36
  • 43