添加新包会破坏.NET 5应用程序 [英] Adding new package breaks the .NET 5 application

查看:70
本文介绍了添加新包会破坏.NET 5应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图弄清楚为什么我的控制台应用程序在我引入新程序包的瞬间失败。使用IdentityModel.OidcClientMicrosoft.AspNetCore.Server.Kestrel有效,但在添加Microsoft.Extensions.Configuration.Json时抛出异常。我也不会在代码中引用新的包,我只是将其添加到项目中。

复制步骤:

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

  2. 升级NetCoreConsoleClient到.NET 5(更新包)。

  3. 删除Serilog.Sinks.Writate过时的包。

  4. 为Program.cs中的SeriLog删除对.WriteTo.LiterateConsole的调用,并添加using IdentityModel.Client

  5. SystemBrowser类中的InvokeAsync方法添加CancellationToken cancellationToken = new CancellationToken()参数。IBrowser接口的签名已更改,新方法应如下所示:public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = new CancellationToken())

  6. 运行应用程序并使用Alice/Alice登录。已成功获取令牌。

  7. 添加包Microsoft.Extensions.Configuration.Json

  8. 运行应用程序。现在写入http响应时引发异常Object reference not set to an instance of an object

写入响应时LoopbackHttpListener.SetResult出现异常:ctx.Response.WriteAsync("<h1>You can now return to the application.</h1>");

为什么仅添加包会对运行时产生如此大的影响?

项目文件:

<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>

完全例外:

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:UsersstefaSourceReposIdentityModel.OidcClient.SamplesNetCoreConsoleClientsrcNetCoreConsoleClientSystemBrowser.cs:line 172

解决方案

去掉Microsoft.AspNetCore.Server.KestrelMicrosoft.Extensions.Configuration.Json,SDK改为Microsoft.NET.Sdk.Web,一切正常。感谢@JHBonarius为我指明了正确的方向。

推荐答案

异常抛出

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

我已经有两条评论了。

  1. WriteAsync是一个异步/任务。您需要等待。
  2. 在这种情况下,你的‘Catch’拦网也会抛出......你对此没有‘圈套’...
无论如何,我认为问题在于您将新包的.NET 5.0版本添加到一个相当旧的项目中(请记住,包通常会引入它们所依赖的其他包)。特别是在ASP.net中,事情并没有那么简单。接口已更改。实例化方法已更改。你经常需要投入更多的工作。

Microsoft.Extentions.Configuration.Json的版本改回v3.1.13,问题似乎消失了...

这篇关于添加新包会破坏.NET 5应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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