question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullReferenceException in .NET6 in Development mode

See original GitHub issue

In .NET6 the framework automatically enables the developer exception page, and I think this is causing a conflict. When debugging in VS I see these errors after an exception.

[2022-02-01 14:37:55.288] [ERR] [Hellang.Middleware.ProblemDetails.ProblemDetailsMiddleware] [R:80000015-0002-fc00-b63f-84710c7967bb|T:40a57b54219fb5047a6056567bda7f3c|S:a5a054639c167772] An exception was thrown attempting to execute the problem details middleware. System.NullReferenceException: Object reference not set to an instance of an object. at Hellang.Middleware.ProblemDetails.ProblemDetailsMiddleware.WriteProblemDetails(HttpContext context, ProblemDetails details) at Hellang.Middleware.ProblemDetails.ProblemDetailsMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)

followed shortly by

[2022-02-01 14:37:55.300] [WRN] [Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware] [R:80000015-0002-fc00-b63f-84710c7967bb|T:40a57b54219fb5047a6056567bda7f3c|S:a5a054639c167772] The response has already started, the error page middleware will not be executed.

and the exceptions bubble up all the way to

[Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer] [R:80000015-0002-fc00-b63f-84710c7967bb|T:40a57b54219fb5047a6056567bda7f3c|S:a5a054639c167772] Connection ID “18158513707758387220”, Request ID “80000015-0002-fc00-b63f-84710c7967bb”: An unhandled exception was thrown by the application.

Best I can tell, the exception is thrown when WriteProblemDetails calls await context.Response.CompleteAsync();, and I believe it has to do with the developer exception page middleware being enabled. I have not found any way in .NET6 to disable the developer exception middleware.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
khellangcommented, Feb 4, 2022

That sounds like a decent solution. I’ll try to find a minimal repro for the NRE and submit an issue/PR to AspNetCore if I manage to reproduce it reliably. I agree with David that it seems like a bug 🐛

0reactions
ErikPilsits-RJWcommented, Feb 4, 2022

I ended up overriding the nswag client generator template to return default(T) on a 204. It’s just the cleanest way to do it, until they come up with an official solution / change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asp.net Core 2.1 NullReferenceException Error in ...
I have a asp.net core 2.1 app that I have recently moved from development to production. The app works perfectly in development with...
Read more >
NullReferenceException Class (System)
A NullReferenceException exception is thrown by a method that is passed null . Some methods validate the arguments that are passed to them....
Read more >
How can I fix the error: System.NullReferenceException
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception ...
Read more >
Completely devoid of details · Issue #3858 · dotnet/runtime ...
NullReferenceExceptions are the most common exceptions your average developer is going to encounter, and yet the only information that is ...
Read more >
NullReferenceException when opening file
NET Core 3.1 solution with C# classes and a SQL project with sql file. When I close the tab and open the file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found