سؤال

I have written a Nancy self-hosted application. This is intended to run as an API without any client interface. It is therefore a console application that is deployed via TopShelf (see code below)

Everything works just fine as long as I run over standard http. However I need to secure this API by running it over https (see SSL Setup section below)

Once I have the service running over https the service hangs after the first call. To be clear, the first call works fine and I receive the correct response. Howvere something must have gone wrong because the second call hangs and only returns after it times-out.

Is this a bug in the Nancy self hosting or have I made a mistake in my code / setup?

Thanks.

The Console Application

public class Program
{
    [STAThread]
    public static void Main()
    {
        HostFactory.Run(config => {
            config.Service<SelfHost>(service =>
            {
                service.ConstructUsing(name => new SelfHost());
                service.WhenStarted(s=> s.Start());
                service.WhenStopped(s=> s.Stop());
            });
            config.RunAsLocalSystem();
            config.StartAutomatically();
        });
    }
}

The Self Hosting Controller

public class SelfHost
{
    private NancyHost nancyHost;

    public void Start()
    {
        var config = new HostConfiguration { 
            UnhandledExceptionCallback = e => Log.Error("Self Host Exception", e) 
        };
        nancyHost = new NancyHost(config, new Uri("https://myurl.com:8081"));
        nancyHost.Start();
    }

    public void Stop()
    {
        nancyHost.Stop();
    }
}

The Nancy Module

public class RootModule : NancyModule
{
    public RootModule()
    {
        Get["/"] = _ =>
        {
            return "Service is Running";
        };
    }
}

SSL Setup

netsh http add sslcert ipport=0.0.0.0:8081 certhash=XXXX880f5e33288a4c88bb1d321d88d44d2XXXX appid={xxxxxxxx-e7e9-xxxx-94dd-5634a472f42d}
netsh http add urlacl url=https://myurl.com:8081/ user=MYDOMAIN\my-admin-user

Edit 1

Following the advice of @Steven Robbins I have now recompiled using the pre-release nuget packages. Unfortunately the latest pre-release packages have not solved the problem, however I do now have some very intersting log information.

Logs

//First Call - success
12:51:10|GET| /
12:51:10|OK | Service is Running    

//Second Call failure                                                                                                                                                                                                                        
12:51:12|Self Host Exception
12:51:12| Method    :AsyncProcessClientCertificate
12:51:12| Message   :Element not found

//Restart Service
12:51:41|Stopping Service
12:51:41|Self Host Exception
12:51:41| Method    :EndGetContext
12:51:41| Message   :The I/O operation has been aborted because of either a thread exit or an application request
12:51:41|Self Host Exception
12:51:41| Method    :EndGetContext
12:51:41| Message   :The I/O operation has been aborted because of either a thread exit or an application request

12:51:43|Starting on https://myurl.net:8081
هل كانت مفيدة؟

المحلول

If you pass in a config object to the NancyHost constructor you can hook uncaught errors - you will probably find that it's exploding due to a change that was made with client certificates which will be fixed in 0.18, but you can get the fix now if you grab it from the CI feed:

http://www.myget.org/gallery/nancyfx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top