C# 带有Nancy的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

我开发了一个Windows服务,它的任务实际上是用特定的
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
。)