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

Closed
thomhurst opened this issue Sep 23, 2024 · 2 comments
Closed

Ambiguous ServiceProvider Extensions #3860

thomhurst opened this issue Sep 23, 2024 · 2 comments
Labels
Area: Testing Platform Belongs to the Microsoft.Testing.Platform core library

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
@Evangelink
Copy link
Member

Hi @thomhurst,

Would it be possible to have a look at the code where this happens? Our extension class does need to be public as it is the way to access the platform service provider system but our extension class is under Microsoft.Testing.Platform.Services so there should be no confusion if you are not mixing up the 2 worlds together.

@Evangelink Evangelink added Needs: Author Feedback Area: Testing Platform Belongs to the Microsoft.Testing.Platform core library and removed Needs: Triage 🔍 labels Sep 30, 2024
@thomhurst
Copy link
Contributor Author

I've ended up removing MEDI now to reduce dependencies in my app. I'll close and if anyone else has the same issue they can raise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Testing Platform Belongs to the Microsoft.Testing.Platform core library
Projects
None yet
Development

No branches or pull requests

2 participants