Misalignment Between AddRedaction Behavior and Expectations #5751
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-compliance
untriaged
Description
Hello,
I have encountered an issue with
Microsoft.Extensions.DependencyInjection.RedactionServiceCollectionExtensions.AddRedaction
. Despite its name,AddRedaction
internally registers theIRedactorProvider
usingTryAddSingleton
instead ofAddSingleton
.Although this may seem like a minor implementation detail, it has significant implications for certain use cases, including mine. Let me elaborate on the scenario:
Context
In our library, we currently use
Microsoft.R9.Extensions.Redaction.IRedactorProvider
but are transitioning to supportMicrosoft.Extensions.Compliance.Redaction.IRedactorProvider
. This change is driven by our need to integrate withHttpClient
logging, where the latter provider is required. Our strategy involves:Microsoft.Extensions.Compliance.Redaction.IRedactorProvider
as an alternative to the R9 provider.Internally, however, some components still rely on the R9 redactor, while others, such as
HttpClient
logging logic, depend on the public redactor. To facilitate this, we implemented the following registration logic:The Problem
When a partner integrates our library and adds
AddRedaction
as follows:We encounter an issue. The internal
TryAddSingleton
logic inAddRedaction
does not execute because our registrations take precedence. Specifically:TryAddSingleton
returnsnull
when the corresponding provider is not already registered (e.g.,Microsoft.R9.Extensions.Redaction.IRedactorProvider
).This issue surfaced during our end-to-end testing and caused unexpected failures. The root cause is that a method named
AddRedaction
implies the use ofAddSingleton
, aligning with the expectation that it actively registers the service. However, the current behavior of usingTryAddSingleton
is counterintuitive and caused integration challenges.Suggested Resolution
To address this issue, I propose the following:
AddRedaction
to useAddSingleton
instead ofTryAddSingleton
. This aligns with its name and makes its behavior more predictable.TryAddSingleton
behavior is necessary for compatibility, consider introducing a separateTryAddRedaction
method that explicitly reflects its intended use.These changes would ensure that the framework adheres to intuitive naming conventions and avoids surprises for developers integrating redaction functionality.
Thank you for considering this issue. Please let me know if you need further details or assistance.
Reproduction Steps
The following steps demonstrate the issue with the current behavior of
AddRedaction
:Expected behavior
The
redactorProvider
should be a valid instance ofMicrosoft.Extensions.Compliance.Redaction.IRedactorProvider
becauseAddRedaction
is expected to actively register the redactor provider usingAddSingleton
.Actual behavior
The
redactorProvider
isnull
. This happens because:TryAddSingleton
registration is executed first and returnsnull
if theR9
redactor provider is not registered.AddRedaction
method internally usesTryAddSingleton
, which does not override the existing registration.Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: