-
Notifications
You must be signed in to change notification settings - Fork 319
Question: How to create instance of service that has constructor parameters. #422
Comments
If there are examples, documentation, kb, wiki pages available that answer my questions, please point me in the correct direction. Thanks |
You need to add |
Hi, thank you for your response. I tested that approach, I still get the same error. I also changed my constructor to expect an ILoggerFactory instead of an ILogger, the same issue. Also, if I just have my constructor expect an ILogger, and use the IServiceProvider::GetRequiredService method, my object is created, and the constructor is called. Further, this call in my code resolves file when a Logger<> is registered:
|
Sorry, I just noticed your |
thanks. that resolved the issue and made me realize, I have a design problem. |
Environment: .NET 4.6.1 (Not Core)
I'm trying to use the new Microsoft.Extensions.DependencyInjections classes with .NET 4.6.1.
I have that has this signature:
In my test harness, I'm creating an instance of the ServiceCollection, registering my interface and implementation.
The call to ActivatorUtilities.CreateInstance fails with the error:
If I remove the string parameter to the constructor, and use _services.GetRequiredService<>(), everything works as expected.
How can I create an instance of a service that requires additional parameters for the constructor?
Thanks
marc
The text was updated successfully, but these errors were encountered: