-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DependencyInjection] IServiceProvider GetService(s) behavior differences #107153
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection |
Check out #54047. Lines 8 to 18 in 2e7ee69
|
@CodeBlanch have you guys considered a different approach to the problem (of detecting local exporter vs global one) that doesn't rely on knowing what has been registered in the container to work? I feel like, in general, explicitly trying to check what is registered and what is not in the container is a code smell. (and yes, @eerhardt suggestion above should work, but I figured I'd drop this here anyways...). |
Closing per workaround of using Also, using the singular |
Doesn't seem like My workaround was to not allow my service to be instantiated by giving it a We'll just have to live with it I guess.
Not for our scenario. I'm calling More details. We have two APIs. One is called "UseOtlpExporter" and the other is called "AddOtlpExpoter". We have rules that say for a given
You have an idea how to do it? Happy to explore alternatives. Don't say statics though 🤣 Because it is perfectly valid to have multiple pipelines in the same process. The |
Ran into a couple of issues in OpenTelemetry .NET: open-telemetry/opentelemetry-dotnet#5537 & open-telemetry/opentelemetry-dotnet#5803
It boils down to behavior differences between M.E.DI and other container implementations.
Let's say I have some service:
When using
Microsoft.Extensions.DependencyInjection
if you ask theIServiceProvider
forMyService
it won't return anything ifMyService
wasn't explicitly added to theIServiceCollection
:This is the behavior we rely on in OpenTelemetry .NET.
But it turns out other containers have different behavior.
This code using
Unity.Microsoft.DependencyInjection
does throw:The reason for that is some containers (reported for Unity & Grace so far) will automatically create services even if they weren't registered into the
IServiceCollection
.Questions...
Is there a better API to use to ask an
IServiceProvider
if some service was registered?Could we add such an API if there is no way to do it?
The text was updated successfully, but these errors were encountered: