Nancy 500服务器错误与dotnetcore和kestrel

时间:2017-02-15 13:19:29

标签: .net-core nancy dotnet-cli

我正在尝试将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服务器错误说 -

  

目前已停用错误详细信息。要启用它,请设置   TraceConfiguration.DisplayErrorTraces为true。例如   覆盖Bootstrapper的Configure方法和调用   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

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

2 个答案:

答案 0 :(得分:6)

这里有两个问题:

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

答案 1 :(得分:0)

.NET Core 3.1 应用程序中将Nancy与Owin >= v3结合使用时,您可能会遇到相同的服务器错误(500)。

  • 我已通过将降级 Microsoft.AspNetCore.Owin从v3.x降级到 v2.2.0。

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

Setup

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

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