Gur*_*Rao 4 c# windows-services nancy
我开发了一个Windows服务,其任务实际上是启动特定的主机url和port.以下是我现在的情况.
Program.cs中
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WindowsDxService()
};
ServiceBase.Run(ServicesToRun);
}
Run Code Online (Sandbox Code Playgroud)
ProjectInstaller.cs
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
配置上serviceProcessInstaller1,并serviceInstaller1在ProjectInstaller.cs [Design]文件中.
serviceProcessInstaller1
Account=LocalSystem
Run Code Online (Sandbox Code Playgroud)
serviceInstaller1
StartType=Automatic
Run Code Online (Sandbox Code Playgroud)
Library.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);
};
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我遵循的this tutorial实际上显示了如何做到这Console application一点我成功但是我希望有这个Windows Service实际上在系统启动时host指定的指定port.该服务已成功启动并正在运行,但每当我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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果使用TopShelf库,您可能会发现编写服务要容易得多.
| 归档时间: |
|
| 查看次数: |
1263 次 |
| 最近记录: |