Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ant 8 posts 79 karma points
    Jul 06, 2020 @ 12:42
    Ant
    0

    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 for ILogger

  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    Jul 06, 2020 @ 13:25
    Søren Kottal
    1

    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...

  • Ant 8 posts 79 karma points
    Jul 06, 2020 @ 14:08
    Ant
    0

    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..

  • Ant 8 posts 79 karma points
    Jul 06, 2020 @ 15:34
    Ant
    0

    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;
        }
    
    ...
    
  • Ant 8 posts 79 karma points
    Jul 06, 2020 @ 15:44
    Ant
    1

    I have solved the problem

    The issue was that I had imported the wrong namespace for ILogger. I am now correctly using using Umbraco.Core.Logging.

    I'll go away and shout at myself for a few hours...

Please Sign in or register to post replies

Write your reply to:

Draft