My code:
public class MyWebService : IDisposable
{
private readonly NancyHost _host;
public MyWebService(int port = 7017)
{
var uri = new Uri(string.Format("http://localhost:{0}", port));
_host = new NancyHost(uri);
_host.Start();
}
public void Dispose()
{
if (_host != null)
{
_host.Stop();
_host.Dispose();
}
}
}
internal class MyWebModule : NancyModule
{
public MyWebModule()
{
Get["/"] = _ => "Received GET request";
}
}
When running following HTTP request: GET http://localhost:7017/
using Insomnia REST client, I'm getting following cryptic exceptions:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot convert type 'Nancy.ErrorHandling.DefaultStatusCodeHandler.DefaultStatusCodeHandlerResult' to 'Nancy.Response''
at CallSite.Target(Closure , CallSite , Object )
With Source: Anonymously Hosted DynamicMethods Assembly
followed up by
Nancy.ViewEngines.ViewNotFoundException
at Nancy.ViewEngines.DefaultViewFactory.GetRenderedView(String viewName, Object model, ViewLocationContext viewLocationContext)
without any additional infromation on exception.
The REST client then shows error 404
.
What have I defined wrong? I've followed the following example: building-a-simple-http-server-with-nancy
If you run your code under debugger - ensure that thrown exception is actually unhandled. Code might throw exceptions and your debugger (if configured to do so) might break on them, even if those exceptions are handled (by some catch
block). Since you receive 404 reply after all and not crash - I guess those exceptions are part of "normal" nancy flow and so are handled.
As for 404 - your module is internal and Nancy will not discover it. Make it public instead:
public class MyWebModule : NancyModule
{
public MyWebModule()
{
Get["/"] = _ => "Received GET request";
}
}