Open thomhurst opened 5 months ago
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection See info in area-owners.md if you want to be subscribed.
https://github.com/dotnet/runtime/issues/41050 is similar.
Moving to Future since we have reached the v9 "feature complete" milestone; API suggestion also needs to be finalized and marked ready-for-review.
Also verify whether this is a full or partial duplicate of https://github.com/dotnet/runtime/issues/41050.
It is a duplicate of #41050 Both the problem and the solution are the same.
Background and motivation
If we register an open generic within the Microsoft Dependency Injection library, we have to also register the implementation as on open-generic type too: e.g.:
services.AddSingleton(typeof(ILogger<>), typeof(MyLogger<>));
If we try and new up an object using the
Func<>
overload, we get an exception. E.g:services.AddSingleton(typeof(ILogger<>), sp => new MyLogger(stringBuilder));
This means we're limited by how this object is created. We can't pass custom arguments to the constructor.
I propose an overload where the
Func<>
also passes theType
information to the delegate, therefore we could interrogate it and use that to create objects.This allows for greater control over objects and more flexibility within the dependency injection setup of an application.
API Proposal
API Usage
Alternative Designs
No response
Risks
No response