The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments

The generic GetService< T> method is an extension method. This means you need to have a :

using Microsoft.Extensions.DependencyInjection;

to allow the compiler to find it.

This method is only meant for optional services. It will return null if the object can't be constructed, either because the type wasn't registered or because some of its dependencies are missing.

GetRequiredService should be used when an application can't work unless a service is available. If an instance can't be created, it will throw an InvalidOperationException.

When that exception is thrown, the full exception text will be a huge help in finding the actual problem. Exceptions thrown in constructors can appear in the Exception.InnerException property. The sequence of calls that ended up in an exception being thrown will appear in the StackTrace property. Calling Exception.ToString() will return a string that contains all of that information for the current exception and any inner exceptions.


It means your compiler only has knowledge of the method that takes a type.

You could call

var incoming = serviceProvider.GetService(typeof(IService));

or you could add a

using Microsoft.Extensions.DependencyInjection;

to make sure your compiler knows the extension method that lets you specify your type as a generic parameter. This might need the package Microsoft.Extensions.DependencyInjection.Abstractions to be installed.