I just attempted to use partially closed generic DI registration for the first time using dotnet 3.1, v3 Azure functions and 3.1.8 of MS DependencyInjection package - all the latest at the time of writing.
The types are resolved as hoped when set as constructor dependencies. However, when I call GetService<> on the container directly I get an ArgumentException:
The number of generic arguments provided doesn't equal the arity of the generic type definition
If I saw this error during runtime I'd conclude that partially closed generics aren't supported, but this isn't the case and I can't figure out why.
public interface IMyInterface<TypeA, TypeB>
{
}
public class MyClass<TypeA> : IMyInterface<TypeA, MyConcreteTypeB>
{
}
services.AddSingleton(typeof(IMyInterface<,>), typeof(MyClass<>));
var myObject = services
.BuildServiceProvider()
.GetService<IMyInterface<MyConcreteTypeA, MyConcreteTypeB>>();
myObject.Should().Be().OfType<MyClass<MyConcreteTypeA>>();
I can't run the (rough) code above as a unit test but I can resolve IMyInterface<MyConcreteTypeA, MyConcreteTypeB> as a constructor param in an Azure Function.
Update:
Working example: https://github.com/mr-panucci/sandbox