NullReferenceException in .NET6 in Development mode
See original GitHub issueIn .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:
- Created 2 years ago
- Comments:15 (7 by maintainers)
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 🐛
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.