简体   繁体   English

添加新的 package 会破坏 .NET 5 应用程序

[英]Adding new package breaks the .NET 5 application

I've been trying to figure out why my console application fails the instant I introduce a new package.我一直试图弄清楚为什么我的控制台应用程序在我引入新的 package 的那一刻就失败了。 Using IdentityModel.OidcClient and Microsoft.AspNetCore.Server.Kestrel only works, but when adding Microsoft.Extensions.Configuration.Json it throws exception.使用IdentityModel.OidcClientMicrosoft.AspNetCore.Server.Kestrel有效,但在添加Microsoft.Extensions.Configuration.Json时会引发异常。 I don't reference the new package in code either, I just add it to the project.我也没有在代码中引用新的 package,我只是将它添加到项目中。

Steps to reproduce:重现步骤:

  1. Clone https://github.com/IdentityModel/IdentityModel.OidcClient.Samples.git克隆https://github.com/IdentityModel/IdentityModel.OidcClient.Samples.git

  2. Upgrade NetCoreConsoleClient to .NET 5 (update packages).NetCoreConsoleClient升级到 .NET 5(更新包)。

  3. Remove Serilog.Sinks.Literate obsolete package.删除Serilog.Sinks.Literate过时的 package。

  4. Remove call to .WriteTo.LiterateConsole for SeriLog in Program.cs and add using IdentityModel.Client .删除 Program.cs 中对.WriteTo.LiterateConsole的 .WriteTo.LiterateConsole 的调用,并using IdentityModel.Client添加。

  5. Add CancellationToken cancellationToken = new CancellationToken() parameter for InvokeAsync method in SystemBrowser class.SystemBrowser class 中的InvokeAsync方法添加CancellationToken cancellationToken = new CancellationToken()参数。 The signature for the IBrowser interface has changed, the new method should look like this: public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = new CancellationToken()) IBrowser接口的签名已更改,新方法应如下所示: public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = new CancellationToken())

  6. Run application and login with alice/alice.运行应用程序并使用 alice/alice 登录。 Acquiring token is successful.获取token成功。

  7. Add package Microsoft.Extensions.Configuration.Json .添加 package Microsoft.Extensions.Configuration.Json

  8. Run application.运行应用程序。 It now throws exception Object reference not set to an instance of an object when writing to the http response.现在,在写入 http 响应时,它会引发异常Object reference not set to an instance of an object

The exception occurs in LoopbackHttpListener.SetResult when writing to the response: ctx.Response.WriteAsync("<h1>You can now return to the application.</h1>");写入响应时LoopbackHttpListener.SetResult发生异常: ctx.Response.WriteAsync("<h1>You can now return to the application.</h1>");

Why does adding a package only , have such an impact to the runtime?为什么添加 package 会对运行时产生如此影响?

Project file:项目文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <AssemblyName>NetCoreConsoleClient</AssemblyName>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
    <PackageReference Include="IdentityModel.OidcClient" Version="3.1.2" />
  </ItemGroup>

</Project>

Complete exception:完全例外:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.AspNetCore.Server.Kestrel.Core
  StackTrace:
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.CreateResponseHeader(Boolean appCompleted)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProduceStart(Boolean appCompleted)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.InitializeResponseAsync(Int32 firstWriteByteCount)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.WriteAsync(ReadOnlyMemory`1 data, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.HttpResponseWritingExtensions.WriteAsync(HttpResponse response, String text, Encoding encoding, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.HttpResponseWritingExtensions.WriteAsync(HttpResponse response, String text, CancellationToken cancellationToken)
   at ConsoleClientWithBrowser.LoopbackHttpListener.SetResult(String value, HttpContext ctx) in C:\Users\stefa\Source\Repos\IdentityModel.OidcClient.Samples\NetCoreConsoleClient\src\NetCoreConsoleClient\SystemBrowser.cs:line 172

Solution解决方案

Get rid of Microsoft.AspNetCore.Server.Kestrel and Microsoft.Extensions.Configuration.Json , change the SDK to Microsoft.NET.Sdk.Web and everything works. Get rid of Microsoft.AspNetCore.Server.Kestrel and Microsoft.Extensions.Configuration.Json , change the SDK to Microsoft.NET.Sdk.Web and everything works. Thanks to @JHBonarius for pointing me in the right direction.感谢@JHBonarius 为我指明了正确的方向。

The exception is thrown in异常被抛出

private void SetResult(string value, HttpContext ctx)
{
    try
    {
        ctx.Response.StatusCode = 200;
        ctx.Response.ContentType = "text/html";
        // below
        ctx.Response.WriteAsync("<h1>You can now return to the application.</h1>"); <--- here
        // ^^
        ctx.Response.Body.Flush();

        _source.TrySetResult(value);
    }
...

I have two comments already.我已经有两条评论了。

  1. WriteAsync is an async/Task. WriteAsync是一个异步/任务。 You need to await it.你需要等待它。
  2. your 'catch' block there will also throw in this case...... you have no 'catch' for that...在这种情况下,您的“catch”块也会抛出......你没有“catch”......

Anyhow, I think the problem is you adding a .NET 5.0 version of the new package (and remember that packages often pull in other packages which they rely on) to a quite old project.无论如何,我认为问题在于您将新 package 的 .NET 5.0 版本(请记住,软件包通常会引入他们所依赖的其他软件包)到一个相当旧的项目中。 Especially with ASP.net it's not that simple.尤其是 ASP.net 并不是那么简单。 Interfaces have changed.接口发生了变化。 Instantiation methods have changed.实例化方法发生了变化。 You often need to put in more work.你经常需要付出更多的努力。

Change the version of Microsoft.Extentions.Configuration.Json back to v3.1.13 and the problem seems to be gone...Microsoft.Extentions.Configuration.Json的版本改回 v3.1.13 问题似乎消失了...

I was having a similar issue with my ASP.NET Core 2.2 project.我的 ASP.NET Core 2.2 项目遇到了类似的问题。 Making sure I didn't have any Microsoft.Extensions.X packages greater than 3.1.X fixed the issue for me.确保我没有任何大于 3.1.X 的 Microsoft.Extensions.X 包为我解决了这个问题。

I had exactly the same issue.我有完全相同的问题。

 ctx.Response.StatusCode = 200;
 ctx.Response.ContentType = "text/html";
 ctx.Response.Headers.Add("Date", new DateTimeOffset.UtcNow.ToString());
 await ctx.Response.WriteAsync(content);
 await ctx.Response.Body.FlushAsync();

Setting the Date Header solved it 4 me, since that is, whats failing in Kestrel.Core设置日期 Header 解决了它 4 我,因为那是,什么在 Kestrel.Core 中失败了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM