在TopShelf下运行Nancy Self Host



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

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

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

netsh http add 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=DOMAINusername

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

看看Topshelf。Nancy还提供NuGet套餐。当你安装服务时,它会为你做URL保留(netsh http)。在卸载服务时,它也将被删除。

  1. 添加Topshelf。南希对你的项目
  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");
});
}

最新更新