AddApiVersioning with a custom IProblemDetailsWriter breaks both the custom and decorated IProblemDetailsWriter #62186
Labels
area-networking
Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Uh oh!
There was an error while loading. Please reload this page.
Is there an existing issue for this?
Describe the bug
Calling
AddApiVersioning()
causes a custom IProblemDetailsWriter to be removed, and a decorated DefaultProblemDetailsWriter to be added to the ServiceProvider while leaving the original in place.The function from "Asp.Versioning.Http"
IServiceCollectionExtensions.TryAddProblemDetailsRfc7231Compliance(...)
correctly looks for and finds the descriptor associated with "DefaultProblemDetailsWriter". However, that function then callsservices.Replace(...)
which always replaces the first enumerable matching the ServiceType. This replaces the custom writer with a decorated version of the default, while still leaving the default writer.ProblemDetailsService iterates through an "IEnumerable" until it finds the first writer where
CanWrite(...)
is true, then stops. This is done in the order the services are registered. To ensure the default writer is not called accidentally, I must register my custom provider before callingAddProblemDetails()
. This triggers the bug.Expected Behavior
My custom
IProblemDetailsWriter
continues to work as expected. If it cannot write, then the DefaultProblemDetailsWriter is augmented byAddApiVersioning()
.Steps To Reproduce
Inspect builder.Services and CustomProblemDetailsWriter will have been removed.
Exceptions (if any)
None
.NET Version
9.0.203
Anything else?
The text was updated successfully, but these errors were encountered: