【问题标题】:NullReferenceException experienced with Owin on Startup .Net Core 2.0 - Settings?在启动 .Net Core 2.0 时使用 Owin 遇到 NullReferenceException - 设置?
【发布时间】:2019-07-24 13:24:33
【问题描述】:

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

我添加到解决方案中的包是

Microsoft.AspNet.WebApi.OwinSelfHost

安装它的依赖项:

Microsoft.AspNet.WebApi.Client

Microsoft.AspNet.WebApi.Core

Microsoft.AspNet.WebApi.Owin

微软.Owin

Microsoft.Owin.Host.HttpListener

Microsoft.Owin.Hosting

Newtonsoft.Json

欧文

我还添加了:

南希

南希·欧文

我的项目属于“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");
    }
}

但是,在启动我的集成测试模块时(通过尝试在其中运行任何测试),我遇到了空引用异常。 这是堆栈跟踪:

System.NullReferenceException : 对象引用未设置为对象的实例。

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

在 Microsoft.Owin.Hosting.Utilities.SettingsLoader.b__0() 在 System.Threading.LazyInitializer.EnsureInitializedCore[T](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 选项)

在 C:\Users[redacted]\source\repos[redacted].IntegrationTests\MyIntegrationTests.cs:line 21 中的 [redacted].IntegrationTests.MyIntegrationTests..ctor()


我尝试过的:

  1. 一一添加不同版本的包。
  2. 更改我的启动类以添加 HttpConfiguration
  3. 清除 localhost cookie(在此处的其他主题中建议)
  4. 使用本指南:https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin#katana---httplistener-selfhost 我收到与以前完全相同的错误。

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

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

【问题讨论】:

标签: c# asp.net-core owin nancy


【解决方案1】:

所以,由于兼容性问题,我这样做的方式似乎是不可能的。 但是,我偶然发现了一种直接配置 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());
    }
}

并将上面的主要测试运行 sn -p 替换为:

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

这现在可以满足我的需要! (响应测试请求的基本“服务器”)

【讨论】:

  • Nancy 不再维护。 :-(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2018-02-18
  • 2018-05-26
  • 2020-12-27
相关资源
最近更新 更多