if (implementationTypeInfo == null || !implementationTypeInfo.IsGenericTypeDefinition)
{
throw new ArgumentException(
Resources.FormatOpenGenericServiceRequiresOpenGenericImplementation(descriptor.ServiceType),
nameof(descriptors));
}
if (implementationTypeInfo.IsAbstract || implementationTypeInfo.IsInterface)
{
throw new ArgumentException(Resources.FormatTypeCannotBeActivated(descriptor.ImplementationType, descriptor.ServiceType));
}
When there's generic type dependency with no concrete generic type injection but instance factory it'll throw the exception. Should be changed to like:
if ((implementationTypeInfo == null || !implementationTypeInfo.IsGenericTypeDefinition) **&& descriptor.ImplementationFactoryWithType == null**)
{
throw new ArgumentException(
Resources.FormatOpenGenericServiceRequiresOpenGenericImplementation(descriptor.ServiceType),
nameof(descriptors));
}
**else** if (implementationTypeInfo.IsAbstract || implementationTypeInfo.IsInterface)
{
throw new ArgumentException(
Resources.FormatTypeCannotBeActivated(descriptor.ImplementationType, descriptor.ServiceType));
}
In the case of using factory declaration of dependency as will be throwing exception "An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.Extensions.DependencyInjection.dll: 'Open generic service type '[name of the type]' requires registering an open generic implementation type.'" in building web host which should be right in where new service descriptor is being added:
public class Startup
{
... ...
public void ConfigureServices(IServiceCollection services)
{
... ...
services.Add[lifecycle type](myGenericType, provider => myFactory.GetInstance(myGenericType));
... ...
}
}
https://github.com/aspnet/DependencyInjection/blob/4708d11dfe2a3df540578f026fc14d11a4f9ef48/src/DI/ServiceLookup/CallSiteFactory.cs#L36
Also refer to: https://github.com/aspnet/Home/issues/3036
Where:
When there's generic type dependency with no concrete generic type injection but instance factory it'll throw the exception. Should be changed to like:
In the case of using factory declaration of dependency as will be throwing exception "An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.Extensions.DependencyInjection.dll: 'Open generic service type '[name of the type]' requires registering an open generic implementation type.'" in building web host which should be right in where new service descriptor is being added:
And a new method should be provided as:
And FactoryCallSite should be extended or new type of IServiceCallSite for factory with requiring type provided in invoking should be implemented.