Copied to clipboard

Flag this post as spam?

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


  • Justin Moser 20 posts 92 karma points
    Aug 27, 2013 @ 17:40
    Justin Moser
    1

    ApplicationEventHandler events not firing.

    I have a custom Umbraco event handler class, that deals with ApplicationStarted/Initialized/Starting events, a long with published events. It also sets up all my dependency injection for my controllers.

    All of a sudden, none of the events are being hit. I was previously inheriting my class from IApplicationEventHandler, and the ApplicationStarted event was being hit correctly. However I changed the file to inherit from ApplicationEventHandler and now nothing is firing. I tried changing it back to IApplicationEventHandler but no use, now no events are firing at all.

    My code below:

    public class UmbracoEventHandler : ApplicationEventHandler
    {
        private static UmbracoApplicationBase _umbracoApplication;
        private static ApplicationContext _applicationContext;
        private static IContentService _contentService;
        private static IEntityDirector _entityDirector;
        private static ContentRepository _contentRepository;
        private static object _lockObj = new object();
        private static bool _ran = false; 
    
        public UmbracoEventHandler()
        {
            _entityDirector = new EntityDirector();
            _contentRepository = new ContentRepository(_entityDirector, umbraco.cms.businesslogic.member.Member.GetCurrentMember().LoginName);
        }
    
        protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
    
        }
    
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            if (!_ran)
            {
              lock (_lockObj)
              {
                if (!_ran)
                {
    
                    _applicationContext = applicationContext;
                    _umbracoApplication = umbracoApplication;
                    _contentService = ApplicationContext.Current.Services.ContentService;
    
                    ConfigureUnity(GlobalConfiguration.Configuration);
                    ConfigureApi(GlobalConfiguration.Configuration);
    
                    Document.AfterPublish += Document_AfterPublish;
                    Document.AfterDelete += Document_AfterDelete;
    
                    _ran = true;
                }
              }
            }
        }
    
        private void Document_AfterDelete(Document sender, umbraco.cms.businesslogic.DeleteEventArgs e)
        {
            if (sender.ContentType.Alias == "BuyProperty" || sender.ContentType.Alias == "RentProperty")
            {
                _contentRepository.DeleteContent(sender);
            }
        }
    
        private void Document_AfterPublish(Document sender, PublishEventArgs publishEventArgs)
        {
            if (sender.ContentType.Alias == "BuyProperty" || sender.ContentType.Alias == "RentProperty")
            {
                _contentRepository.InsertOrUpdateContent(sender);
            }
        }
    
        private void ConfigureUnity(HttpConfiguration config)
        {
            UnityContainer unity = new UnityContainer();
            unity.RegisterType<PropertiesApiController>();
            unity.RegisterType<IEntityDirector, EntityDirector>(new HierarchicalLifetimeManager());
            config.DependencyResolver = new IoCContainer(unity);
        }
    
        private void ConfigureApi(HttpConfiguration config)
        {
            var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
        }
    

    I cant for the life of me figure out why none of the events have suddenly stopped firing. It's especially weird as previously they were firing fine.

    Also, in my updated class, I have set breakpoints on all the methods, including the constructor, but it only ever makes it into the constructor, never the event methods.

    Btw, I am using umbraco v6.1.1.

    Thanks, Justin

  • Justin Moser 20 posts 92 karma points
    Sep 02, 2013 @ 17:31
    Justin Moser
    101

    Turned out it was having a constructor that screwed it up...duh!

  • Douglas Ludlow 210 posts 366 karma points
    Aug 13, 2014 @ 20:52
    Douglas Ludlow
    0

    @JustinMoser, you got me right on track. What got me was a line I had at the beginning of the class:

    private UmbracoHelper Umbraco = new UmbracoHelper(UmbracoContext.Current);

    After I moved that into my handler method, then it was happy and my events were fired. Only took me a few hours to troublehoot, so I'm documenting it here for sanity's sake.

  • Simon 692 posts 1068 karma points
    Sep 05, 2016 @ 07:36
    Simon
    0

    Hi guys,

    I am trying to inject dependencies in my custom Umbraco event handler class and I am using Simple Injector MVC for Dependency injection.

    But I cannot figure out what I should because ApplicationStarted is not being hit during debugging.

    when I remove the dependencies from constructor, it works. But I need such service to accomplish such task.

    public class MemberServiceEventsHandler : ApplicationEventHandler
        {
    
            private bool _memberApprovalStatusHasBeenChanged;
            private readonly ISetMemberThatApprovalHasBeenDoneAtLeastOnceService _setMemberThatApprovalHasBeenDoneAtLeastOnceService;
    
            public MemberServiceEventsHandler(ISetMemberThatApprovalHasBeenDoneAtLeastOnceService setMemberThatApprovalHasBeenDoneAtLeastOnceService)
            {
                _setMemberThatApprovalHasBeenDoneAtLeastOnceService = setMemberThatApprovalHasBeenDoneAtLeastOnceService;
            }
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                MemberService.Saving += MemberService_Saving;
                MemberService.Saved += MemberService_Saved;
            }
    
            private void MemberService_Saving(IMemberService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMember> e)
            {
                foreach (var node in e.SavedEntities)
                {
                    if (node.ContentType.Alias == ConstantValues.DefaultMemberTypeAlias)
                    {
                        _memberApprovalStatusHasBeenChanged = node.IsPropertyDirty(ConstantValues.MemberIsApprovedAliasName);
                    }
                }
            }
    }
    

    In Controllers, dependencies are working just fine but when I come to inject dependencies in this class, it is not working :/

    Does anyone what I am missing please?

    Thank you

    Kind Regards

  • Ivan 165 posts 543 karma points
    Feb 27, 2017 @ 08:40
    Ivan
    0

    Hi Simon,

    Have you managed to solve the problem? The dependency injection doesn't seem to work for ApplicationEventHandler, not even in the newest v 7.5.9.

  • Rob Carlaw 21 posts 134 karma points
    May 31, 2017 @ 15:12
    Rob Carlaw
    0

    It could be because the IoC code hasn't been fired yet.

    I had several startup classes that all inherited ApplicationEventHandler. However you can't be sure of the order in which they're run.

Please Sign in or register to post replies

Write your reply to:

Draft