Skip to content
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

Ambiguous ServiceProvider Extensions #3860

Open
thomhurst opened this issue Sep 23, 2024 · 0 comments
Open

Ambiguous ServiceProvider Extensions #3860

thomhurst opened this issue Sep 23, 2024 · 0 comments

Comments

@thomhurst
Copy link
Contributor

I've got Microsoft.Extensions.DependencyInjection as a dependency.

When I call ServiceProvider.GetRequiredService<T>() then I get an IDE error about ambiguous extensions.

There's the standard DI:

        public static T GetRequiredService<T>(this IServiceProvider provider) where T : notnull
        {
            ThrowHelper.ThrowIfNull(provider);

            return (T)provider.GetRequiredService(typeof(T));
        }

And a Testing Platform one:

        public static TService GetRequiredService<TService>(this IServiceProvider provider)
        where TService : notnull
    {
        Guard.NotNull(provider);

        object? service = ((ServiceProvider)provider).GetService(typeof(TService));
        ApplicationStateGuard.Ensure(service is not null, string.Format(CultureInfo.InvariantCulture, PlatformResources.ServiceProviderCannotFindServiceErrorMessage, typeof(TService)));

        return (TService)service;
    }

Do your testing platform ones need to be public? As a quick fix would be to make them internal.

Because even if I tried to use it, it crashes with the exception:

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope' to type 'Microsoft.Testing.Platform.Services.ServiceProvider'.
   at Microsoft.Testing.Platform.Services.ServiceProviderExtensions.GetRequiredService[TService](IServiceProvider provider) in /_/src/Platform/Microsoft.Testing.Platform/Services/ServiceProviderExtensions.cs:line 40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant