C# 带有Nancy的Windows服务未启动主机
我开发了一个Windows服务,它的任务实际上是用特定的C# 带有Nancy的Windows服务未启动主机,c#,windows-services,nancy,C#,Windows Services,Nancy,我开发了一个Windows服务,它的任务实际上是用特定的url和端口启动主机。下面是我现在拥有的 Program.cs static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WindowsDxService() }; ServiceBase.Run(ServicesToRun); } [RunInstall
url
和端口启动主机。下面是我现在拥有的
Program.cs
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WindowsDxService()
};
ServiceBase.Run(ServicesToRun);
}
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
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();
}
}
}
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);
};
}
}
ProjectInstaller.cs
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WindowsDxService()
};
ServiceBase.Run(ServicesToRun);
}
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
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();
}
}
}
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);
};
}
}
WindowsDxService.cs
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WindowsDxService()
};
ServiceBase.Run(ServicesToRun);
}
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
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();
}
}
}
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);
};
}
}
在ProjectInstaller.cs[Design]
文件中的serviceProcessInstaller1
和serviceInstaller1
上进行配置
serviceProcessInstaller1
Account=LocalSystem
Library.cs
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WindowsDxService()
};
ServiceBase.Run(ServicesToRun);
}
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
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();
}
}
}
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);
};
}
}
基本上,我遵循了,它实际上展示了如何使用控制台应用程序来实现这一点,尽管我成功了,但我想让它成为Windows服务
,它在系统启动时实际启动带有指定端口的主机
。该服务已成功启动并正在运行,但每当我在同一系统中浏览url时,它都不会显示页面,这意味着我们的基本此网页不可用
消息。要启动主机
,我还必须执行哪些配置?希望得到帮助。您启动服务时正在处理主机。我建议这样做:
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库,您可能会发现编写服务要容易得多。是否Host.Start
return?如果是这样,您将使用OnStart
方法处理主机。如果没有,那么您的服务将无法正常启动。建议您在停止服务时进行处理。@JackHughes表示歉意,但我今天真的开始使用这项windows服务
和Nancy
,对此我所知甚少。如果即使服务停止,我也必须继续执行此host.Start()
,该怎么办。。服务的任务应该是只启动主机
。在启动
中分配主机
并调用启动
方法。在OnStop
中处置主机。主机将是WindowsDxService
类中的一个字段。您好,Guruprasad Rao,您可以将工作代码上载到GitHub以供参考吗?它有助于选择@Jack。。你搞定了……)非常感谢。我也将尝试使用TopShelf
。)