C# Owin selfhost asp.net Web api在作为服务启动时拒绝连接

C# Owin selfhost asp.net Web api在作为服务启动时拒绝连接,c#,asp.net-web-api,service,owin,self-host-webapi,C#,Asp.net Web Api,Service,Owin,Self Host Webapi,我有一个工作的WebAPI服务,我从一个控制台项目开始。 现在我想让它作为服务运行 我使用installutil.exe将其作为服务安装,代码如下: [RunInstaller(true)] public class ApplicationInstaller:Installer { private ServiceProcessInstaller processInstaller; private ServiceInstaller serviceInstaller;

我有一个工作的WebAPI服务,我从一个控制台项目开始。 现在我想让它作为服务运行

我使用installutil.exe将其作为服务安装,代码如下:

[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);


    }



}
这是我用来启动服务的:

     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();
        }
    }
publicstaticvoidstartservice()
{
var startOptions=新的startOptions();
var internalURL=$“http://{Environment.MachineName}:6666”;
添加(内部URL);
_Debug($“服务启动于:{internalURL}”);
startOptions.url.Add(“http://localhost:6666");
foreach(Dns.GetHostEntry(Dns.GetHostName()).AddressList中的var地址)
{
if(address.AddressFamily==AddressFamily.InterNetwork)
{
var ipURL=$“http://{address.ToString()}:6666”;
_Debug($“服务启动于:{ipURL}”);
startOptions.url.Add(ipURL);
}
}
使用(WebApp.Start(startOptions))
{
WriteLine($“服务已启动并在{internalURL}侦听”);
Console.ReadLine();
}
}
当我启动服务时,我的记录器显示它一直通过StartService()运行。 我可以看到我已经添加了我在客户端调用的ip。 服务在我启动后没有停止

但是,当我连接时,我收到以下消息: “无法建立连接,因为目标计算机主动拒绝了它”

由于帐户类型是ServiceAccount.LocalSystem,并且它确实启动(并且不会引发异常),因此特权应该是ok的。 我已经为我使用的端口在本地防火墙中添加了一个例外

还有什么不对劲

编辑:
在命令窗口中运行netstat-ano,webservice作为控制台和服务运行,当作为服务运行时,它不会显示在列表中,当作为控制台应用运行时,它会显示在列表中。防火墙配置问题?

正常。我已经找到了解决方案(或问题) 首先:当作为服务运行控制台应用程序时,编译器跳过console.WriteLine和console.ReadLine(我想整个控制台类型都被忽略了)

这意味着程序将退出我的using语句:

    using(WebApp.Start<SelfHostConfiguration>(startOptions))
    {
        Console.WriteLine($"Service started and listening at {internalURL}");
        Console.ReadLine();
    }
使用(WebApp.Start(startOptions))
{
WriteLine($“服务已启动并在{internalURL}侦听”);
Console.ReadLine();
}
这显然是不好的,因为服务器已被释放。 使调试变得困难的是,即使没有Console.ReadLine,服务在停止之前也不会退出。如果它是一个普通的控制台应用程序,它就会退出。 因此,解决方案就是这样启动服务:


\u webapp=webapp.Start(“http://+:8080”)

正常。我已经找到了解决方案(或问题) 首先:当作为服务运行控制台应用程序时,编译器跳过console.WriteLine和console.ReadLine(我想整个控制台类型都被忽略了)

这意味着程序将退出我的using语句:

    using(WebApp.Start<SelfHostConfiguration>(startOptions))
    {
        Console.WriteLine($"Service started and listening at {internalURL}");
        Console.ReadLine();
    }
使用(WebApp.Start(startOptions))
{
WriteLine($“服务已启动并在{internalURL}侦听”);
Console.ReadLine();
}
这显然是不好的,因为服务器已被释放。 使调试变得困难的是,即使没有Console.ReadLine,服务在停止之前也不会退出。如果它是一个普通的控制台应用程序,它就会退出。 因此,解决方案就是这样启动服务:


\u webapp=webapp.Start(“http://+:8080”)

您需要使用ServiceBase.Run(…)并实现ServiceBase类。在从Windows服务主机接收到停止之前,ServiceBase.Run(…)是一个阻塞调用。
示例。

您需要使用ServiceBase.Run(…)并实现ServiceBase类。在从Windows服务主机接收到停止之前,ServiceBase.Run(…)是一个阻塞调用。 例如