Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nancy 500 server error with dotnetcore and kestrel

I am trying to use NancyFX (clint-eastwood) with dotnetcore1.1 and dotnet-cli 1.0.0-rc4-004771. My current project structure is -

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

And codes are -

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 is currently empty.

When I try to access Get("/test/{name}", args => new Person() { Name = args.name }); from a rest client i get the expected result.

However, when I try to access to root or Get("/", _ => { return View["index.sshtml"]; });, I get a 500 server error saying -

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)

I tried following the instruction in the error message and enable error tracing by including the following code in CustomBootstrapper.cs

protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, IPipelines pipelines)
{
    var environment = GetEnvironment();
    environment.Tracing(true, true);
}

But then I get the following error when trying to run the application with 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

I am not sure what's causing the error or how to enable tracing. Can anyone help?

like image 731
th1rdey3 Avatar asked Mar 09 '23 20:03

th1rdey3


1 Answers

The are two problems here :

  • The 500 is because the view was not found , what you need to do is provide a root path by implementing IRootPathProvider and return Directory.GetCurrent().
  • Secondly to enable tracing you need public override void Configure(INancyEnvironment environment) this adds the keys hence you the exception you are getting.
like image 53
Sifiso Shezi Avatar answered Mar 13 '23 05:03

Sifiso Shezi