Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    May 15, 2019 @ 15:54
    Ismail Mayat
    0

    Events no longer being handled

    I have the following class:

        public class MembershipEvents : ApplicationEventHandler
    {
        private IAtocMemberService _memberService;
        private IIndiciaService _indiciaService;
    
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            _memberService = DependencyResolver.Current.GetService<IAtocMemberService>();
            _indiciaService = DependencyResolver.Current.GetService<IIndiciaService>();
    
            _memberService.MemberCreated += MemberService_MemberCreated;
            _memberService.MemberUpdated += MemberService_MemberUpdated;
            _memberService.MemberDeleted += MemberService_MemberDeleted;
        }
    
        private void MemberService_MemberCreated(IAtocMember member)
        {
            _indiciaService.CreateMember(member);
        }
    
        private void MemberService_MemberUpdated(IAtocMember member)
        {
            _indiciaService.UpdateMember(member);
        }
    
        private void MemberService_MemberDeleted(IAtocMember member)
        {
            _indiciaService.DeleteMember(member, member.Email);
        }
    }
    

    Inside one of my services i have:

    code like

            public void OnMemberCreated(IAtocMember member)
        {
            if (MemberCreated != null)
            {
                MemberCreated(member);
            }
        }
    

    and further down:

                bool isCreated = _membersRepository.CreateMember(member);
    
            if (isCreated)
            {
                OnMemberCreated(member);
    
                return MembershipCreateStatus.Success;
            }
    

    this all used to work, we have made some code changes but not anywhere round here. So the event is raised and when i look at MemberCreated it is always null, even though it has been wired up . So it looks like there is no listener to the event.

    One thing to note the services are registered using lightinject.

    Anyone any ideas?

    Regards

    Ismail

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    May 16, 2019 @ 09:11
    Ismail Mayat
    100

    Ok fixed my issue. It was due to the way I was registering my service in the container. There was no lifetime scope set so it would have defaulted to request scope and hence the instance with the wired events was not the one that was passed into the other service.

    Anyhow changing to :

                container.Register<IAtocMemberService, AtocMemberService>(new PerContainerLifetime());
    

    fixed it

Please Sign in or register to post replies

Write your reply to:

Draft