Erik83
Erik83

Reputation: 549

Owin selfhost asp.net Web api refuse connection when started as a service

I have a working web api service that I start with a console project. Now I want to have it running as a service.

Im installing it as a service using installutil.exe with this code:

[RunInstaller(true)]
 public    class ApplicationInstaller:Installer
{
    private ServiceProcessInstaller processInstaller;
    private ServiceInstaller serviceInstaller;

    public ApplicationInstaller()
    {

        serviceInstaller = new ServiceInstaller();
        processInstaller = new ServiceProcessInstaller();
        processInstaller.Account = ServiceAccount.LocalSystem;

        serviceInstaller.StartType = ServiceStartMode.Automatic;
        serviceInstaller.ServiceName = "WTT Rumsa";
        serviceInstaller.Description = "Web API for WTT Rumsa";

        Installers.Add(serviceInstaller);
        Installers.Add(processInstaller);


    }



}

This is what I use to start the service:

     public static void StartService()
    {
        var startOptions = new StartOptions();
        var internalURL = $"http://{Environment.MachineName}:6666";
        startOptions.Urls.Add(internalURL);
        _logger.Debug($"Service started at: {internalURL}");

        startOptions.Urls.Add("http://localhost:6666");


        foreach(var address in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
        {
            if(address.AddressFamily==AddressFamily.InterNetwork)
            {
                var ipURL = $"http://{address.ToString()}:6666";

                _logger.Debug($"Service started at: {ipURL}");

                startOptions.Urls.Add(ipURL);


            }
        }

        using(WebApp.Start<SelfHostConfiguration>(startOptions))
        {
            Console.WriteLine($"Service started and listening at {internalURL}");
            Console.ReadLine();
        }
    }

When I start the service my logger shows that it runs all the way through the StartService(). I can see that I've added the ip that Im calling in my client. The service does not stop after I started it.

However when I connect I get this message: "No connection could be made because the target machine actively refused it"

Since the account type is ServiceAccount.LocalSystem and it does actually start (and not throw an exception) the privileges should be ok. I've added an exception to my local firewall for the port that Im using.

What else could be wrong?

Edit: Ran netstat -ano in command window with the webservice running as console and service and when its run in as a service it does not show up in the list and when run as a console app it does. Firewall config problem?

Upvotes: 0

Views: 2325

Answers (2)

Michael
Michael

Reputation: 3631

You need to use ServiceBase.Run(...) and implemented a ServiceBase class. ServiceBase.Run(...) is a blocking call until it receiveds a stop from the Windows Service Host. Example here.

Upvotes: 1

Erik83
Erik83

Reputation: 549

Ok. I've found the solution (or the problems) First of all: when running a console app as a service the compiler skips the Console.WriteLine and Console.ReadLine (I guess the entire Console type is just ignored)

This means that the program will step out of my using statement:

    using(WebApp.Start<SelfHostConfiguration>(startOptions))
    {
        Console.WriteLine($"Service started and listening at {internalURL}");
        Console.ReadLine();
    }

And that is obviously bad since the server is disposed. What made it difficult to debug was the fact that the service does not exit until stopped, even without the Console.ReadLine. If it were a normal Console application it would have exited. So the solution was just to start the service like this:

_webapp = WebApp.Start<SelfHostConfiguration>("http://+:8080");

Upvotes: 2

Related Questions