使用 Nancy 自托管 + 的空白回复顶层货架

发布于 2024-11-17 04:20:32 字数 2150 浏览 2 评论 0原文

我正在尝试在 Topshelf 服务中使用 Nancy(使用带有 Razor 视图 nuget 包的自托管)。我将其托管在 http://localhost:8585/ 上,当我处于调试模式或直接调用我的可执行文件时它工作得很好。 Nancy 模块完美地提供了剃刀视图。

然后我安装该应用程序:

myapp.exe install

当我启动该服务时,它似乎工作正常,并且没有错误。然后,当我在浏览器中访问 http://localhost:8585/ 时,我得到一个空白响应。有什么想法吗?

在开始使用 Topshelf 托管服务之前,我启动 Nancy:

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

之后,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();
}

我使用 ninject,StartService 和 StopService 方法只是打印 stopwatch.ElapsedMilliseconds 当前值的函数。

这是我的 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);
};

我使用 Nancy 中的所有默认设置,除了自托管和模块之外。剃须刀零件。知道会发生什么吗?

我还尝试了 netsh http add urlacl url=http://+:8585/ user=Everyone ,它似乎对行为没有任何影响。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听你说爱我 2024-11-24 04:20:32

我冒险猜测这是一个根路径问题,因此它找不到您的视图(假设正在复制视图)。我不知道 TopShelf 是如何工作的 - 它将工作目录设置为它正在启动的应用程序的目录吗?默认情况下,根路径将设置为“Environment.CurrentDirectory”,这可能正确也可能不正确。

如果使用 CurrentDirectory 不可行,那么您可以切换到嵌入视图(有关如何在主解决方案中执行此操作的示例),或者将 IRootPathProvider 的实现添加到您的项目中并返回程序集位置(它将自动获取您的版本)超过默认值)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文