The parameterless constructor is needed, but AFAIK never used.
So if you add your parameterless constructor, and assign Current.Logger to your logger property, then you should be good to go. At least, that is how I have solved this...
This does not work. The system will not resolve the dependency for ILogger:
-> System.InvalidOperationException: Unresolved dependency [Target Type: XXX.Core.Events.NotifyWhenMemberApprovedComponent], [Parameter: logger(ClientDependency.Core.Logging.ILogger)], [Requested dependency: ServiceType:ClientDependency.Core.Logging.ILogger, ServiceName:]
at LightInject.ServiceContainer.GetEmitMethodForDependency(Dependency dependency) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4207
at LightInject.ServiceContainer.EmitConstructorDependency(IEmitter emitter, Dependency dependency) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4154
at LightInject.ServiceContainer.EmitConstructorDependencies(ConstructionInfo constructionInfo, IEmitter emitter, Action1 decoratorTargetEmitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4120
at LightInject.ServiceContainer.EmitNewInstanceUsingImplementingType(IEmitter emitter, ConstructionInfo constructionInfo, Action1 decoratorTargetEmitMethod) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4081
at LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter emitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4038
at LightInject.ServiceContainer.<>cDisplayClass153_0.0(IEmitter ms) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856
at LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action`1 serviceEmitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3777
at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4743
public class NotifyWhenMemberApprovedComponent : IComponent
{
private readonly ILogger _logger;
private readonly IScopeProvider _scopeProvider;
public NotifyWhenMemberApprovedComponent(ILogger logger, IScopeProvider scopeProvider)
{
_logger = logger;
_scopeProvider = scopeProvider;
}
...
Dependency Injection for Event Handlers
I'm following the tutorial here: https://our.umbraco.com/documentation/getting-started/Code/Subscribing-To-Events/
This tutorial says you can inject the ILogger interface into the constructor of the component.
However, when I do this I get a build error. It works if I include a parameterless constructor, but then I can't acess the logger.
What am I doing wrong?
EDIT: I resolved this by using the correct
using
statement forILogger
The parameterless constructor is needed, but AFAIK never used.
So if you add your parameterless constructor, and assign Current.Logger to your logger property, then you should be good to go. At least, that is how I have solved this...
I will try this again.
I did add both a parameterless constructor and one with the ILogger param. But it always used the parameterless constructor..
This does not work. The system will not resolve the dependency for ILogger:
-> System.InvalidOperationException: Unresolved dependency [Target Type: XXX.Core.Events.NotifyWhenMemberApprovedComponent], [Parameter: logger(ClientDependency.Core.Logging.ILogger)], [Requested dependency: ServiceType:ClientDependency.Core.Logging.ILogger, ServiceName:] at LightInject.ServiceContainer.GetEmitMethodForDependency(Dependency dependency) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4207 at LightInject.ServiceContainer.EmitConstructorDependency(IEmitter emitter, Dependency dependency) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4154 at LightInject.ServiceContainer.EmitConstructorDependencies(ConstructionInfo constructionInfo, IEmitter emitter, Action
1 decoratorTargetEmitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4120 at LightInject.ServiceContainer.EmitNewInstanceUsingImplementingType(IEmitter emitter, ConstructionInfo constructionInfo, Action
1 decoratorTargetEmitMethod) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4081 at LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter emitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4038 at LightInject.ServiceContainer.<>cDisplayClass153_0.0(IEmitter ms) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856 at LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action`1 serviceEmitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3777 at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4743I have solved the problem
The issue was that I had imported the wrong namespace for
ILogger
. I am now correctly usingusing Umbraco.Core.Logging
.I'll go away and shout at myself for a few hours...
is working on a reply...