Reputation: 12102
When trying to instantiate
public class MyClass : BackgroundService {
private ILogger<MyClass> Logger {get;}
public MyClass(Logger<MyClass> logger) {
Logger = logger
}
}
through .Net DI by registering MyClass
through
services.AddHostedService<MyClass>();
When the class is activated, an exception is thrown:
System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.Extensions.Logging.Logger
1[MyClass]' while attempting to activate 'MyClass'.'`
I understood that ILogger<T>
should be automatically available for all types T
.
What should I do to fix this?
Upvotes: 1
Views: 244
Reputation: 12102
While ILogger<T>
is automatically available, Logger<T>
isn't.
Change the constructor parameter from Logger<MyClass>
to ILogger<MyClass>
and then everything should work.
Upvotes: 1