我正在尝试启动WebApp以在.NET Core 2.0项目中与NancyFx一起使用。
我添加到解决方案中的包是这样做的
Microsoft.AspNet.WebApi.OwinSelfHost
它安装了它的依赖项:
Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.Owin
Microsoft.Owin
Microsoft.Owin.Host.HttpListener
Microsoft.o win.hosting
Newtonsoft.Json
Owin
我还补充说:
南希
Nancy.Owin
我的项目是“xUnit Test Project(.NET Core)”类型。
从我的测试课开始,我们有:
public class MyIntegrationTests : IDisposable
{
private readonly IDisposable _webApp;
private const string Url = "http://localhost:1234";
public MyIntegrationTests()
{
_webApp = WebApp.Start<Startup>(url: Url);
}
我的启动课看起来像:
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
appBuilder.UseNancy();
}
}
我还有一个带测试路线的NancyModule:
public class TestModule : NancyModule
{
public TestModule()
{
Get("/test", args => "test");
}
}
但是,在启动Integration Tests模块时(通过尝试在其中运行任何测试),我遇到了Null Reference Exception。这是堆栈跟踪:
System.NullReferenceException:未将对象引用设置为对象的实例。
在Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor()
在System.Threading.LazyInitializer.EnsureInitializedCore [T]的Microsoft.Owin.Hosting.Utilities.SettingsLoader.b__0()中(T&target,Func`1 valueFactory)
在Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary`2设置)
在Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions选项)
在Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions选项)
在Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions选项)
at [redacted] .IntegrationTests.MyIntegrationTests..ctor()在C:\ Users [编辑] \ source \ repos [编辑] .IntegrationTests \ MyIntegrationTests.cs:第21行
我尝试过的:
对我来说,看起来有一个配置缺失 - 或者没有找到。但是,我提到的一切都存在。有任何想法吗? (值得一提 - 这个测试项目没有appsettings.json,web.config等)
编辑:测试项目在这里可用:https://www.dropbox.com/s/v1bw5pu9t0e9fwt/NancyOwinTest.zip?dl=0制作测试项目,我意识到它是在.NET 4.6.1级别而不是.NET Core上恢复软件包。我可能犯了一个愚蠢的错误,但是哪一个,我还没弄明白。
因此,由于兼容性问题,我似乎无法做到这一点。但是,我偶然发现了一种直接配置csproj文件以引用正确软件包的方法,这里:https://github.com/NancyFx/Nancy/issues/2863#issuecomment-365107613
在这里复制配置可能会发生变化:
<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>
与启动类结合使用:
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.UseOwin(x => x.UseNancy());
}
}
上面的主要测试运行代码段替换为:
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();
}
NancyModule保持不变:
public class TestModule : NancyModule
{
public TestModule()
{
Get("/test", args => "test");
}
}
这现在适合我的需求! (响应测试目的请求的基本“服务器”)