【问题标题】:Running Nancy Self Host under TopShelf在 TopShelf 下运行 Nancy Self Host
【发布时间】:2016-09-20 22:52:41
【问题描述】:

我在工作中使用 topShelf(Windows 7)编写了一个 Nancy 自托管服务,它工作得很好。我把它带回家并在 Windows 10 下运行它,我收到以下错误:

Nancy 自身主机无法启动,因为提供的 url 不存在命名空间预留。

请在提供给的 HostConfiguration 上启用 UrlReservations.CreateAutomatically NancyHost,或使用(提升的)命令手动创建预订:

netsh http 添加 urlacl url="http://+:5000/" user="Everyone"

我看到了这个建议:

HostConfiguration hostConfigs = new HostConfiguration()
{
    UrlReservations = new UrlReservations() { CreateAutomatically = true }
};

但它似乎只在运行您自己的主机时才有效,而不是在 TopShelf 上。这是我的主要代码:

    public static void Main()
    {
        HostFactory.Run(x =>
        {
            //x.UseLinuxIfAvailable();
            x.Service<SelfHost>(s =>
            {
                s.ConstructUsing(name => new SelfHost());
                s.WhenStarted(tc => tc.Start());
                s.WhenStopped(tc => tc.Stop());
            });

            x.RunAsLocalSystem();
            x.SetDescription("SciData Recipe Data Interaction");
            x.SetDisplayName("SciData.Recipe Service");
            x.SetServiceName("SciData.Recipe");
        });
    }

有人可以建议如何解决此问题,使其在 Windows 10 下运行吗?谢谢...

更新: 以下确实有效:以管理员身份运行命令 shell 并输入以下内容似乎可以使一切正常。

netsh http add urlacl url=http://+:1234/ user=DOMAIN\username

其中 1234 是服务使用的端口。我仍然想在代码中找出如何做到这一点,但如果这不起作用,这就足够了。

【问题讨论】:

    标签: nancy topshelf


    【解决方案1】:

    查看Topshelf.Nancy 也可作为 NuGet 包提供。当您安装服务时,它会为您执行 URL 保留 (netsh http)。卸载服务时也会被删除。

    1. 将 Topshelf.Nancy 添加到您的项目中
    2. 将“WithNancyEndpoint”添加到您的服务中

    您的代码:

    public static void Main()
    {
        HostFactory.Run(x =>
        {
            //x.UseLinuxIfAvailable();
            x.Service<SelfHost>(s =>
            {
                s.ConstructUsing(name => new SelfHost());
                s.WhenStarted(tc => tc.Start());
                s.WhenStopped(tc => tc.Stop());
                s.WithNancyEndpoint(x, c =>
                {
                    c.AddHost(port: 1234);
                    c.CreateUrlReservationsOnInstall();
                });
            });
    
            x.RunAsLocalSystem();
            x.SetDescription("SciData Recipe Data Interaction");
            x.SetDisplayName("SciData.Recipe Service");
            x.SetServiceName("SciData.Recipe");
        });
    }
    

    【讨论】:

    • 谢谢,但也许值得一提防火墙选项和 onuninstall 规则以及 nc.AddHost(port: 8080); nc.CreateUrlReservationsOnInstall(); nc.DeleteReservationsOnUnInstall(); nc.OpenFirewallPortsOnInstall(firewallRuleName: "myservice"); nc.Bootstrapper = new Bootstrapper();
    猜你喜欢
    • 2012-01-22
    • 2011-11-03
    • 2016-10-27
    • 2014-05-25
    • 1970-01-01
    • 2018-05-11
    • 2011-09-22
    • 1970-01-01
    • 2010-12-02
    相关资源
    最近更新 更多