Vin*_*ent 3 c# kestrel-http-server identitymodel .net-5
我一直试图找出为什么我的控制台应用程序在引入新包后立即失败。仅使用IdentityModel.OidcClientand有效,但添加时会引发异常。我也不在代码中引用新包,我只是将其添加到项目中。Microsoft.AspNetCore.Server.Kestrel 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.
为类中的方法添加CancellationToken cancellationToken = new CancellationToken()参数。接口的签名已更改,新方法应如下所示:InvokeAsyncSystemBrowserIBrowserpublic async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = new CancellationToken())
运行应用程序并使用 alice/alice 登录。获取token成功。
添加包Microsoft.Extensions.Configuration.Json。
运行应用程序。Object reference not set to an instance of an object现在写入 http 响应时会引发异常。
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>
Run Code Online (Sandbox Code Playgroud)
完整异常:
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
Run Code Online (Sandbox Code Playgroud)
去掉Microsoft.AspNetCore.Server.Kestrel和Microsoft.Extensions.Configuration.Json,将 SDK 更改为Microsoft.NET.Sdk.Web,一切正常。感谢@JHBonarius 为我指明了正确的方向。
我有完全相同的问题。
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = "text/html";
ctx.Response.Headers.Add("Date", DateTimeOffset.UtcNow.ToString());
await ctx.Response.WriteAsync(content);
await ctx.Response.Body.FlushAsync();
Run Code Online (Sandbox Code Playgroud)
设置日期标头解决了这个问题 4 我,因为这就是 Kestrel.Core 中失败的地方