Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank responses using Nancy self host + TopShelf

Tags:

c#

topshelf

nancy

I am trying to use Nancy (using the Self Hosting with Razor views nuget packages) inside of a Topshelf service. I am hosting it on http://localhost:8585/, and it works just fine while I'm in debug mode or call my executable directly. The Nancy module serves up a razor view perfectly.

I then install the application:

myapp.exe install

When I start the service, it seems to be working just fine, and there are no errors. Then, when I go to http://localhost:8585/ in the browser I get a blank response. Any ideas as to why?

Before I start hosting the service with Topshelf, I start up Nancy:

_nancyHost = new NancyHost(_baseUri);
_nancyHost.Start();

After that, the topshelf service is configured and started like this:

using (Container)
{
    var host = HostFactory.New(config =>
    {
        config.EnableDashboard();
        config.AfterStartingServices(() => Console.WriteLine("Done starting..."));
        config.Service<EventHandlerService>(s =>
        {
            s.SetServiceName("EventHandlerService");
            s.ConstructUsing(c => Container.Get<EventHandlerService>());
            s.WhenStarted(n => StartService(n, stopwatch));
            s.WhenStopped(n => StopService(n, stopwatch));
        });
        config.RunAsLocalSystem();
        config.SetDescription("A service for processing events.");
        config.SetDisplayName("EventHandlerService");
        config.SetInstanceName("EventHandlerService");
        config.SetServiceName("EventHandlerService");
    });
    host.Run();
}

I am using ninject, and the StartService and StopService methods are just functions that print the current value of stopwatch.ElapsedMilliseconds.

Here's the configuration of my Nancy module:

Get["/"] = parameters =>
{
    var indexViewModel = new IndexViewModel { CurrentDateTime = DateTime.Now, WorkLog = _service.WorkLog };

    return View["index", indexViewModel];
};

Get["/js/{file}"] = p =>
{
    return Response.AsJs("scripts/" + p.file as String);
};

Get["/style/{file}"] = p =>
{
    return Response.AsCss("content/themes/base/" + p.file as String);
};

Get["/img/{file}"] = p =>
{
    return Response.AsImage("content/themes/base/images/" + p.file as String);
};

I'm using all of the defaults in Nancy, other than the Self Hosting & Razor parts. Any idea what might be going on?

I also tried netsh http add urlacl url=http://+:8585/ user=\Everyone and it didn't seem to have any affect on the behavior.

like image 395
John Nelson Avatar asked Jun 24 '11 14:06

John Nelson


1 Answers

I'd hazard a guess at it being a root path problem so it can't find your view (assuming the views are being copied). I've no idea how TopShelf works - does it set the working directory to the directory of the app it's launching? By default the root path will be set to "Environment.CurrentDirectory" which may or may not be correct.

If using CurrentDirectory isn't feasible then you can either switch to embedded views (example on how to do that in the main solution), or add an implementation of IRootPathProvider to your project and return the assembly location instead (it will automatically pickup your version over the default)

like image 143
Steven Robbins Avatar answered Sep 20 '22 16:09

Steven Robbins