I've been trying to figure out why my console application fails the instant I introduce a new package. Using IdentityModel.OidcClient
and Microsoft.AspNetCore.Server.Kestrel
only works, but when adding Microsoft.Extensions.Configuration.Json
it throws exception. I don't reference the new package in code either, I just add it to the project.
Steps to reproduce:
Clone https://github.com/IdentityModel/IdentityModel.OidcClient.Samples.git
Upgrade NetCoreConsoleClient to .NET 5 (update packages).
Remove Serilog.Sinks.Literate obsolete package.
Remove call to
.WriteTo.LiterateConsole
for SeriLog in Program.cs and addusing IdentityModel.Client
.Add
CancellationToken cancellationToken = new CancellationToken()
parameter forInvokeAsync
method inSystemBrowser
class. The signature for theIBrowser
interface has changed, the new method should look like this:public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = new CancellationToken())
Run application and login with alice/alice. Acquiring token is successful.
Add package
Microsoft.Extensions.Configuration.Json
.Run application. It now throws exception
Object reference not set to an instance of an object
when writing to the http response.
The exception occurs in LoopbackHttpListener.SetResult
when writing to the response: 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?
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. Thanks to @JHBonarius for pointing me in the right direction.