NancyFx/Nancy — Issue #419: Self hosting - navigating to URL without trailing / causes exception
Author: jfberry
Created: 2011-11-18 15:42:06+00:00
State: open

I am using Nancy 0.9.0 from NuGet

I am self hosting, like this:

Nancy.Hosting.Self.NancyHost host = new Nancy.Hosting.Self.NancyHost(new Uri("http://localhost:80/Nancy/"));
host.Start();

Simple starter module:

public class StartModule : NancyModule
{
public StartModule()
{
Get["/"] = _ => "Hello World!";

}
}

Then I went to test...

http://localhost/Nancy/ --> works!
http://localhost/Nancy/test --> nancy 404 page, great!
http://localhost/Nancy --> CRASH!!

The nancy process is torn down with an exception:

Unhandled Exception: System.InvalidOperationException: Unable to locate base URI for request: http://localhost/Nancy
at Nancy.Hosting.Self.NancyHost.ConvertRequestToNancyRequest(HttpListenerRequest request)
at Nancy.Hosting.Self.NancyHost.Process(HttpListenerContext ctx)
at Nancy.Hosting.Self.NancyHost.GotCallback(IAsyncResult ar)
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
at System.Net.ListenerAsyncResult.WaitCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

On advice from grumpydev, attempted to construct the NancyHost without the trailing slash - but this fails inside the constructor:

System.ArgumentException occurred
Message=Only Uri prefixes ending in '/' are allowed.
Parameter name: uriPrefix
Source=System
ParamName=uriPrefix
StackTrace:
at System.Net.HttpListener.AddPrefix(String uriPrefix)
at System.Net.HttpListenerPrefixCollection.Add(String uriPrefix)
at Nancy.Hosting.Self.NancyHost..ctor(INancyBootstrapper bootStrapper, Uri[] baseUris)
at Nancy.Hosting.Self.NancyHost..ctor(Uri[] baseUris)
at Fusion.Web.Startup.Go() in C:\source\fusion\FilterPOC_v2\Fusion.Web\Startup.cs:line 16
InnerException: