Copied to clipboard

Flag this post as spam?

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


  • wgaddis 8 posts 78 karma points
    Jul 06, 2018 @ 20:20
    wgaddis
    0

    Injecting references via interface to ApplicationEventHandler.ApplicationStarted()/ApplicationStarting

    I'm trying to use a service (that's used elsewhere, and I'm injecting via autofac in some controllers) in a method for a published event. I've made an ApplicationEventHandler, and I've overridden ApplicationStarting(this is the one below, but I've tried both)/Started methods, and it doesn't seem like it's even possible to inject dependencies to this class. Having a constructor makes the functions I'm adding to the ContentService not work/get called at all, and I'd rather not instantiate the service with the eventhandler if at all possible.

    My code is below, I guess what I'm asking is "Can I inject services/dependencies to avoid without instantiating the classes in my ApplicationEventHandler", or "What is another approach/pattern for creating event handlers that update third party services on publish"

    I've seen a few other threads where people are trying to do this, but none of them really seem to have gotten anywhere/don't have any more replies.

    public class UmbracoEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Published += ContentServicePublished;
                base.ApplicationStarting(umbracoApplication, applicationContext);
            }
    
            private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
            {
                LogHelper.Info(typeof(UmbracoEventHandler), "Something has been published");
                foreach (var node in args.PublishedEntities)
                {
                    if (node.Published)
                    {
                        //other logic and send to service here
                    }
                }
            }
        }
    
  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 09, 2018 @ 11:24
    Ismail Mayat
    0

    Where are you wiring up the services via autofac?

    Also in content service event can you not get instance of the service via the container? Not used autofac but have used lightinject and ninject and as well as injecting services into controllers i have retrieved services via the container.

    Regards

    Ismail

  • wgaddis 8 posts 78 karma points
    Jul 09, 2018 @ 14:35
    wgaddis
    0

    Services are set to the container in my global file/UmbracoApplication class().

    Not sure what you mean by "get services from the container". I'm not sure where else I can define my container that the constructor in the ApplicationEventHandler will be found. I tried using the Starting method instead of the started method override, but no dice (I don't see why that would have changed the access/order of the constructor being called though).

Please Sign in or register to post replies

Write your reply to:

Draft