kroax
kroax

Reputation: 249

cant start nancy self host without admin rights

My app uses Nancy Selfhosting. When I launch it without admin rights I get a System.Net.HttpListenerException "Access Denied".

Here is the code:

static void Main(string[] args)
    {   
        var nancyHost = new Nancy.Hosting.Self.NancyHost(new Uri("http://localhost:80/"));
        nancyHost.Start();
        Application.Run();
    }

I have also tried different ports without success. Strangely, I dont get any Exceptions when launching a HttpListener that listens to the same Url. What could be causing this exception?

Upvotes: 18

Views: 9008

Answers (3)

robpvn
robpvn

Reputation: 897

You need to set the self-host configuration to not rewrite the localhost route via the RewriteLocalhost property.

namespace NancyApplication1
{
    using System;
    using Nancy.Hosting.Self;

    class Program
    {
        static void Main(string[] args)
        {
            var uri = new Uri("http://localhost:3579");
            var config = new HostConfiguration();

            // (Change the default RewriteLocalhost value)
            config.RewriteLocalhost = false;

            using (var host = new NancyHost(config, uri))
            {
                host.Start();

                Console.WriteLine("Your application is running on " + uri);
                Console.WriteLine("Press any [Enter] to close the host.");
                Console.ReadLine();
            }
        }
    }
}

I found this out by trying and failing a bit, but this page explains the reason behind.

Upvotes: 47

Ashtonian
Ashtonian

Reputation: 4423

Alternatively - From the documentation:

Note that on Windows hosts a HttpListenerException may be thrown with an Access Denied message. To resolve this the URL has to be added to the ACL. Also but the port may need to be opened on the machine or corporate firewall to allow access to the service.

Add to ACL by running the following command:

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

if you need to remove from ACL:

netsh http delete urlacl url=http://+:8080/

Upvotes: 4

Jeffrey Zhao
Jeffrey Zhao

Reputation: 5023

You can hosting Nancy with Kestrel. It's really simple:

public void Main(string[] args)
{
    var owinHost = new WebHostBuilder()
        .UseStartup<Startup>()
        .UseUrls("http://+:12345/")
        .Build();

    owinHost.Run();
}

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseOwin(x => x.UseNancy());
    }
}

The only difficulty is to prepare all the dlls (30+) required. We should definitely use NuGet to resolve all the dependencies.

Upvotes: 0

Related Questions