Open MigueZS opened 1 month ago
@MigueZS Thanks for reporting this issue!
I believe the problem is a result of the logic that the ProblemDetailsService
employs to determine which ProblemDetailsWriter
it should use to emit the ProblemDetails
object:
When AddControllers
is called, it registers the default implementation used by MVC, which is defined here.
When AddProblemDetails
is called, it registers its own default implementation.
The Write
logic in each of these is slightly different which is what I suspect is causing this issue. I suspect in this particular case it's MVC's ProblemDetailsWriter
that is being called since it doesn't support setting a trace ID. To resolve this, we'll need to update the DefaultProblemDetailsFactory to emit a trace ID as extension data.
Would you be interested in submitting a PR with this fix?
Thank you for your response.
I found that your suggestion has already been applied: https://github.com/dotnet/aspnetcore/commit/9f4eb97c12a227edbba99751af3b5ac741634c93. Please correct me if I am wrong.
If so can you please answer the following questions:
Is there an existing issue for this?
Describe the bug
A .NET 8.0 application with ProblemDetails enabled not showing the traceId when AddProblemDetails is called before AddControllers. This happens in both, when using the WebApplicationBuilder or using the CreateDefaultWebhost with an startup class. This only happens when the Accept header sent by the client is application/json.
Expected Behavior
When an exception occurs in one controller, the problem details response shows the traceId or the documentation clearly warns you to call AddProblemDetails after the AddControllers call.
Order is important in middleware but not when configuring services.
Steps To Reproduce
Create a new .net 8.0 application with the following Program.cs
Create a controller that throws an exception
Call the endpoint, the traceId is shown regardless of the Accept header sent
Swap the order of the AddControllers and AddProblemDetails calls in the Program.cs
Call the endpoint and the traceId is not shown when the Accept header is "application/json"
Any other Accept header will produce the traceId
Exceptions (if any)
No response
.NET Version
8.0.400
Anything else?
Tested in Visual Studio 2022 or running dotnet run from the console.
Dotnet info output