【发布时间】:2021-03-27 09:36:08
【问题描述】:
我一直试图弄清楚为什么我的控制台应用程序在我引入一个新包时会失败。使用 IdentityModel.OidcClient 和 Microsoft.AspNetCore.Server.Kestrel only 有效,但添加 Microsoft.Extensions.Configuration.Json 时会引发异常。我也没有在代码中引用新包,我只是将它添加到项目中。
重现步骤:
-
克隆https://github.com/IdentityModel/IdentityModel.OidcClient.Samples.git
-
将 NetCoreConsoleClient 升级到 .NET 5(更新包)。
-
删除 Serilog.Sinks.Literate 过时的包。
-
删除 Program.cs 中对 SeriLog 的
.WriteTo.LiterateConsole的调用并添加using IdentityModel.Client。 -
在
SystemBrowser类中为InvokeAsync方法添加CancellationToken cancellationToken = new CancellationToken()参数。IBrowser接口的签名已更改,新方法应如下所示:public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = new CancellationToken()) -
运行应用程序并使用 alice/alice 登录。获取token成功。
-
添加包
Microsoft.Extensions.Configuration.Json。 -
运行应用程序。现在在写入 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:\Users\stefa\Source\Repos\IdentityModel.OidcClient.Samples\NetCoreConsoleClient\src\NetCoreConsoleClient\SystemBrowser.cs:line 172
解决方案
摆脱Microsoft.AspNetCore.Server.Kestrel和Microsoft.Extensions.Configuration.Json,将SDK更改为Microsoft.NET.Sdk.Web,一切正常。感谢@JHBonarius 为我指明了正确的方向。
【问题讨论】:
-
请在第 5 步中提供更多详细信息。您似乎在声明一个局部变量,但您说您正在添加一个参数......究竟抛出了什么?
-
请注意,仅升级软件包的主要版本并将核心 2.0 升级到 5.0 并删除软件包,而不修改代码,也可能是原因。这是不稳定行为的主要风险。
-
需要更多修改。需要在 Program.cs 中添加
using IdentityModel.Client;并需要从 Program.cs 中删除.WriteTo.LiterateConsole(outputTemplate:...。 -
@JHBonarius,我添加了更多上下文,我也尝试过不升级存储库,但通过启动一个新项目并复制相关代码。使用正确的包。
-
请添加抛出的异常。
标签: c# .net-5 kestrel-http-server identitymodel