简体   繁体   中英

Running Nancy Self Host under TopShelf

I wrote a Nancy Self-Hosted service using topShelf at work (windows 7) and it worked just fine. I brought it home and run it under Windows 10, and I get the following error:

The Nancy self host was unable to start, as no namespace reservation existed for the provided url(s).

Please either enable UrlReservations.CreateAutomatically on the HostConfiguration provided to the NancyHost, or create the reservations manually with the (elevated) command(s):

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

I saw this suggestion:

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

But it only seems to work when running your own host, not with TopShelf. Here's my Main code:

    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");
        });
    }

Can someone suggest how to fix this so it runs under Windows 10? Thanks...

UPDATE: The following did work: running a command shell as admin and typing the following seems to make everything work.

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

Where 1234 is the port the service uses. I'd still like to find out how to do this in the code, but if that doesn't work, this will suffice.

Take a look to Topshelf.Nancy also available as a NuGet package. It's doing the URL Reservation (netsh http) for you when you install the service. It will also be deleted when the service is uninstalled.

  1. Add Topshelf.Nancy to your project
  2. Add "WithNancyEndpoint" to your service

Your code:

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");
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM