带有 Nancy 的 Windows 服务不会启动主机
本文关键字:启动 主机 服务 Nancy Windows 带有 | 更新日期: 2023-09-27 18:35:54
我开发了一个Windows服务,其任务实际上是启动具有特定url
和port
的主机。以下是我现在所拥有的。
程序.cs
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WindowsDxService()
};
ServiceBase.Run(ServicesToRun);
}
项目安装程序.cs
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
WindowsDxService.cs
public partial class WindowsDxService : ServiceBase
{
public WindowsDxService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
var url = "http://127.0.0.1:9000";
using (var host = new NancyHost(new Uri(url)))
{
host.Start();
}
}
}
serviceProcessInstaller1
上的配置,并在ProjectInstaller.cs [Design]
文件中serviceInstaller1
。
serviceProcessInstaller1
Account=LocalSystem
serviceInstaller1
StartType=Automatic
图书馆.cs
public class Library : NancyModule
{
public Library()
{
Get["/"] = parameters =>
{
return "Hello world";
};
Get["jsontest"] = parameters =>
{
var test = new
{
Name = "Guruprasad Rao",
Twitter="@kshkrao3",
Occupation="Software Developer"
};
return Response.AsJson(test);
};
}
}
基本上我遵循了this tutorial
它实际上显示了如何使用我成功Console application
做到这一点,但我想将其作为Windows Service
,它实际上在系统启动时以指定的port
启动host
。该服务已成功启动并正在运行,但每当我在同一系统中浏览url
时,它都不会显示页面,这意味着我们的基本This webpage is not available
消息。我还需要做什么配置才能启动host
?希望得到帮助。
启动服务时正在释放主机。我会建议这样的事情:
public partial class WindowsDxService : ServiceBase
{
private Host host;
public WindowsDxService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.host = new NancyHost(...)
this.host.Start();
}
protected override void OnStop()
{
this.host.Stop();
this.host.Dispose();
}
}
如果您使用 TopShelf 库,您可能会发现编写服务要容易得多。