使用 dotnetcore 和 kestrel 的 Nancy 500 服务器错误

Nancy 500 server error with dotnetcore and kestrel

我正在尝试将 NancyFX (clint-eastwood) 与 dotnetcore1.1dotnet-cli 1.0.0-rc4-004771 一起使用。我当前的项目结构是 -

CustomBootstrapper.cs
HomeModule.cs
index.sshtml
nancyapp.csproj
Program.cs
Startup.cs

代码是-

nancyapp.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Owin">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Nancy">
      <Version>2.0.0-clinteastwood</Version>
    </PackageReference>
  </ItemGroup>
</Project>

Program.cs

using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

namespace nancyapp
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

Startup.cs

using Microsoft.AspNetCore.Builder;
using Nancy.Owin;

namespace nancyapp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseOwin(x => x.UseNancy());
        }
    }
}

HomeModule.cs

using Nancy;

namespace nancyapp
{
    public class HomeModule : NancyModule
    {
        public HomeModule()
        {
            Get("/", _ => { return View["index.sshtml"]; });
            Get("/test/{name}", args => new Person() { Name = args.name });
        }
    }

    public class Person
    {
        public string Name { get; set; }
    }
}

index.sshtml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    Welcome to Nancy App.
</body>
</html>

CustomBootstrapper.cs 目前为空。

当我尝试从休息客户端访问 Get("/test/{name}", args => new Person() { Name = args.name }); 时,我得到了预期的结果。

但是,当我尝试访问 root 或 Get("/", _ => { return View["index.sshtml"]; }); 时,我收到 500 服务器错误消息 -

Error details are currently disabled. To enable it, please set TraceConfiguration.DisplayErrorTraces to true. For example by overriding your Bootstrapper's Configure method and calling environment.Tracing(enabled: false, displayErrorTraces: true)

我尝试按照错误消息中的说明进行操作,并通过在 CustomBootstrapper.cs

中包含以下代码来启用错误跟踪
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, IPipelines pipelines)
{
    var environment = GetEnvironment();
    environment.Tracing(true, true);
}

但是当我尝试使用 dotnet run

运行 应用程序时出现以下错误
Unhandled Exception: System.ArgumentException: An item with the same
  key has already been added. Key: Nancy.TraceConfiguration at
  System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(Object key) at 
  System.Collections.Generic.Dictionary`2.Insert(TKey key,TValue value, Boolean add) at
  nancyapp.CustomBootstrapper.ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) in D:\TempWork\nancyapp\CustomBootstrapper.cs:line 17 at
  Nancy.Bootstrapper.NancyBootstrapperBase`1.Initialise() at
  Nancy.Owin.NancyMiddleware.UseNancy(NancyOptions options) at
  Nancy.Owin.DelegateExtensions.UseNancy(Action`1 builder, NancyOptionsoptions) at 
  nancyapp.Startup.<>c.<Configure>b__0_0(Action`1 x) in D:\TempWork\nancyapp\Startup.cs:line 10 at
  Microsoft.AspNetCore.Builder.OwinExtensions.UseOwin(IApplicationBuilder builder, Action`1 pipeline) at
  nancyapp.Startup.Configure(IApplicationBuilder app) in D:\TempWork\nancyapp\Startup.cs:line 10
  --- End of stack trace from previous location where exception was thrown --- at
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at
  Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app) at
  Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() at
  Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at
  nancyapp.Program.Main(String[] args) in D:\TempWork\nancyapp\Program.cs:line 11

我不确定导致错误的原因或如何启用跟踪。有人可以帮忙吗?

这里有两个问题:

  • 500 是因为未找到视图,您需要做的是通过实施 IRootPathProvider 和 return Directory.GetCurrent().
  • 提供根路径
  • 其次,要启用跟踪,您需要 public override void Configure(INancyEnvironment environment) 这会添加密钥,因此您将获得异常。

.NET Core 3.1 应用程序中结合 Owin >= v3.

使用 Nancy 时,您可能会遇到相同的服务器错误 (500)
  • 我已经通过降级 Microsoft.AspNetCore.Owin 从 v3.x 到 v2.2.0.[ 解决了这个问题=30=]

我的 运行 降级后设置如下:

也可以return一个简单的文本进行测试:

Get("/", _ => { return new TextResponse(HttpStatusCode.OK, "Hello world!"); });