Owin在启动.Net Core 2.0上遇到过NullReferenceException-设置吗? [英] NullReferenceException experienced with Owin on Startup .Net Core 2.0 - Settings?

查看:323
本文介绍了Owin在启动.Net Core 2.0上遇到过NullReferenceException-设置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启动一个WebApp以在.NET Core 2.0项目中与NancyFx一起使用.

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

Microsoft.AspNet.WebApi.OwinSelfHost

安装依赖项:

Microsoft.AspNet.WebApi.Client

Microsoft.AspNet.WebApi.Client

Microsoft.AspNet.WebApi.Core

Microsoft.AspNet.WebApi.Core

Microsoft.AspNet.WebApi.Owin

Microsoft.AspNet.WebApi.Owin

Microsoft.Owin

Microsoft.Owin

Microsoft.Owin.Host.HttpListener

Microsoft.Owin.Host.HttpListener

Microsoft.Owin.Hosting

Microsoft.Owin.Hosting

Newtonsoft.Json

Newtonsoft.Json

欧文

我还添加了:

南希

Nancy.Owin

Nancy.Owin

我的项目的类型为"xUnit测试项目(.NET Core)".

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);
    }

我的启动类如下:

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        appBuilder.UseNancy();
    }
}

我还有一个带有测试路线的NancyModule:

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:对象引用未设置为对象的实例.

System.NullReferenceException : Object reference not set to an instance of an object.

在Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor()

at Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor()

在System.Threading.LazyInitializer.EnsureInitializedCore [T](T&目标,Func`1 valueFactory)处

at Microsoft.Owin.Hosting.Utilities.SettingsLoader.b__0() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Func`1 valueFactory)

在Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary`2设置)

at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary`2 settings)

在Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions选项)

at Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions options)

在Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions选项)

at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions options)

在Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions选项)

at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options)

位于[已编辑] .IntegrationTests.MyIntegrationTests..ctor()在C:\ Users [已编辑] \ source \ repos [已编辑] .IntegrationTests \ MyIntegrationTests.cs:第21行

at [redacted].IntegrationTests.MyIntegrationTests..ctor() in C:\Users[redacted]\source\repos[redacted].IntegrationTests\MyIntegrationTests.cs:line 21


我尝试过的事情:


What i've tried:

  1. 一个版本一个版本地添加软件包.
  2. 更改我的启动类以添加HttpConfiguration
  3. 清除localhost cookie(在此处的其他主题中建议)
  4. 使用本指南: https ://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin#katana---httplistener-selfhost 我收到了与以前完全相同的错误.
  1. Adding packages one by one, with varying versions.
  2. Changing my Startup Class to add a HttpConfiguration
  3. Clearing localhost cookies (was suggested in other topics here)
  4. Using this guide: https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin#katana---httplistener-selfhost i receive the exact same error as previously.

在我看来,似乎缺少配置-或找不到配置.但是,我所指的一切都存在.有任何想法吗? (值得一提的是-这个测试项目没有appsettings.json,web.config等)

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)

此处提供测试项目: https://www. dropbox.com/s/v1bw5pu9t0e9fwt/NancyOwinTest.zip?dl=0 在进行测试项目时,我意识到它是在.NET 4.6.1级别而不是.NET Core上还原软件包. 我可能犯了一个愚蠢的错误,但是我还没有弄清楚哪一个.

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.

推荐答案

因此,由于兼容性问题,似乎我无法做到这一点. 但是,我偶然发现了一种直接配置csproj文件以引用正确的软件包的方法,在这里:

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>

结合启动类:

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();
        }

NancyModule保持不变:

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)

这篇关于Owin在启动.Net Core 2.0上遇到过NullReferenceException-设置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆