Is there a way to use .NET Core dependency injection to create a concrete type without that type being registered as a service?
For example, say I have a SummaryReportService
class. It is not registered as a service. However, its constructor takes several parameters that are registered as a service.
Is there any way to create an instance of this class and have dependency injection provide all the services needed by the constructor?
CodePudding user response:
Maybe you are not doing a good design. But, I found cases in wich I had to do something like that. You will need a reference to the IServiceProvider, and do something like:
var summaryReportService = ActivatorUtilities.CreateInstance(this.serviceProvider, typeof(SummaryReportService));
It will create a instance of SummaryReportService with the required dependencies (if configured).