简体   繁体   English

使用Nancy self host + TopShelf的空白响应

[英]Blank responses using Nancy self host + TopShelf

I am trying to use Nancy (using the Self Hosting with Razor views nuget packages) inside of a Topshelf service. 我试图在Topshelf服务中使用Nancy(使用Razor查看nuget包的Self Hosting)。 I am hosting it on http://localhost:8585/ , and it works just fine while I'm in debug mode or call my executable directly. 我在http:// localhost:8585 /上托管它,它在我处于调试模式或直接调用我的可执行文件时工作正常。 The Nancy module serves up a razor view perfectly. Nancy模块完美呈现剃刀视图。

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. 然后,当我在浏览器中访问http:// localhost:8585 /时,我得到一个空白的回复。 Any ideas as to why? 任何想法为什么?

Before I start hosting the service with Topshelf, I start up Nancy: 在我开始使用Topshelf托管服务之前,我启动Nancy:

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

After that, the topshelf service is configured and started like this: 之后,topshelf服务配置并启动如下:

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. 我使用的是ninject,StartService和StopService方法只是打印stopwatch.ElapsedMilliseconds当前值的函数。

Here's the configuration of my Nancy module: 这是我的Nancy模块的配置:

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. 我正在使用Nancy中的所有默认设置,而不是Self Hosting和Razor部件。 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. 我也尝试过netsh http add urlacl url=http://+:8585/ user=\\Everyone并且它似乎没有对行为产生任何影响。

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? 我不知道TopShelf是如何工作的 - 它是否将工作目录设置为它正在启动的应用程序的目录? By default the root path will be set to "Environment.CurrentDirectory" which may or may not be correct. 默认情况下,根路径将设置为“Environment.CurrentDirectory”,这可能是也可能不正确。

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) 如果使用CurrentDirectory不可行,那么您可以切换到嵌入式视图(如何在主解决方案中执行此操作的示例),或者将IRootPathProvider的实现添加到项目中并返回组装位置(它将自动拾取您的版本超过默认值)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM