问题
I'm trying to start up a WebApp to use with NancyFx in a .NET Core 2.0 project.
The package i've added to the solution to do this is
Microsoft.AspNet.WebApi.OwinSelfHost
which installs it's dependencies:
Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.Owin
Microsoft.Owin
Microsoft.Owin.Host.HttpListener
Microsoft.Owin.Hosting
Newtonsoft.Json
Owin
I've also added:
Nancy
Nancy.Owin
My project is of type "xUnit Test Project (.NET Core)".
Starting with my test class, we have:
public class MyIntegrationTests : IDisposable
{
private readonly IDisposable _webApp;
private const string Url = "http://localhost:1234";
public MyIntegrationTests()
{
_webApp = WebApp.Start<Startup>(url: Url);
}
My startup class looks like:
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
appBuilder.UseNancy();
}
}
I also have a NancyModule with a test route:
public class TestModule : NancyModule
{
public TestModule()
{
Get("/test", args => "test");
}
}
However, when starting my Integration Tests module (by trying to run any test within it), i am met with a Null Reference Exception. This is the stack trace:
System.NullReferenceException : Object reference not set to an instance of an object.
at Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor()
at Microsoft.Owin.Hosting.Utilities.SettingsLoader.b__0() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Func`1 valueFactory)
at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary`2 settings)
at Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions options)
at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions options)
at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options)
at [redacted].IntegrationTests.MyIntegrationTests..ctor() in C:\Users[redacted]\source\repos[redacted].IntegrationTests\MyIntegrationTests.cs:line 21
What i've tried:
- Adding packages one by one, with varying versions.
- Changing my Startup Class to add a HttpConfiguration
- Clearing localhost cookies (was suggested in other topics here)
- Using this guide: https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin#katana---httplistener-selfhost i receive the exact same error as previously.
To me, it looks like there is a configuration missing - or not being found. However, everything i refer to exists. Any ideas? (Worth mentioning - this test project has no appsettings.json, web.config, etc)
EDIT: Test project available here: https://www.dropbox.com/s/v1bw5pu9t0e9fwt/NancyOwinTest.zip?dl=0 Making the test project, i realise it's restoring packages at the .NET 4.6.1 level, rather than .NET Core. I may well be making a stupid mistake, but which one, i haven't figured out yet.
回答1:
So, it seems the way i was doing this was not possible, due to compatibility issues. However, i stumbled upon a way to configure the csproj file directly to reference the correct packages, here: https://github.com/NancyFx/Nancy/issues/2863#issuecomment-365107613
Copying config here incase that goes down:
<Project Sdk="Microsoft.NET.Sdk.Web" ToolsVersion="15.0">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<DebugType>portable</DebugType>
<AssemblyName>nancydemo</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>nancydemo</PackageId>
<RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>
<StartupObject>NancyApplication.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Owin" Version="2.0.1" />
<PackageReference Include="Nancy" Version="2.0.0-barneyrubble" />
</ItemGroup>
</Project>
In combination with the startup class:
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.UseOwin(x => x.UseNancy());
}
}
and the main test run snippet above replaced with:
public class MyIntegrationTests : IDisposable
{
private readonly IWebHost _webApp;
private const string Url = "http://localhost:1234";
public MyIntegrationTests ()
{
_webApp = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.UseUrls(Url)
.Build();
_webApp.Start();
}
The NancyModule stayed the same:
public class TestModule : NancyModule
{
public TestModule()
{
Get("/test", args => "test");
}
}
This now works for my needs! (A basic 'server' responding to requests for test purposes)
来源:https://stackoverflow.com/questions/54962797/nullreferenceexception-experienced-with-owin-on-startup-net-core-2-0-settings