我有一個可用的web api服務,我從一個控制檯項目開始。 現在我想讓它作爲服務運行。Owin selfhost asp.net Web api作爲服務啓動時拒絕連接
進出口使用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();
}
}
當我開始了我的服務記錄顯示它通過StartService()一直運行。 我可以看到我已經添加了我在客戶端調用的IP。 服務在我啓動後不會停止。
然而,當我連接我得到這個消息: 「無連接可以作出,因爲目標機器積極地拒絕它」
由於帳戶類型ServiceAccount.LocalSystem,它並真正開始(而不是拋出例外)特權應該沒問題。 我爲我使用的端口在本地防火牆中添加了一個異常。
還有什麼可能是錯的?
編輯: 在命令窗口中運行netstat -ano時,webservice作爲控制檯和服務運行,當它作爲服務運行時,它不會顯示在列表中,並以控制檯應用程序的形式運行。防火牆配置問題?