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?