Blank responses using Nancy self host + TopShelf
Asked Answered
S

1

5

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.

Sociability answered 24/6, 2011 at 14:22 Comment(0)
F
8

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)

Floristic answered 24/6, 2011 at 14:33 Comment(6)
That's a very interesting thought. I'll have a look at that. Thanks!Sociability
That was it. I added Environment.CurrentDirectory = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;and it worked like a charm. Thanks again, Steven!Sociability
No problem - roll on the day we add diagnostics :-)Floristic
Topshelf, by default, does not change the current working directory. However, it's possible to get that jammed from either a module you are using (all app domains in the same process share the same working directory) or if the service installs with a different working directory for some reason.Fortney
@Fortney Yeah that's a good point. Steven reminded me on twitter that Nancy supports embedded views, which should remove the dependency on Environment.CurrentDirectory.Sociability
I've expanded my answer to cover a few ways around it if CurrentDirectory is going to be problematic.Floristic

© 2022 - 2024 — McMap. All rights reserved.