Copied to clipboard

Flag this post as spam?

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


  • Sébastien Richer 194 posts 430 karma points
    Jan 07, 2013 @ 19:44
    Sébastien Richer
    0

    Wiring up events with IApplicationEventHandler

    Hello,

    I used to have this class that extended ApplicationStartupHandler to wire up Document.AfterPublish += AfterPublish; this event. Now mre Resharper tells me I should use IApplicationEventHandler instead. Anyone has experience with this? This interface wants me to implement these:

    public void OnApplicationInitialized(UmbracoApplication httpApplication, ApplicationContext applicationContext)
    {
    }
    
    public void OnApplicationStarting(UmbracoApplication httpApplication, ApplicationContext applicationContext)
    {
    }
    
    public void OnApplicationStarted(UmbracoApplication httpApplication, ApplicationContext applicationContext)
    {
    }

    But really all I want is to register my event...

    Not sure what I'm asking here :D

    Thanks!

    Seb

  • Dirk De Grave 4539 posts 6013 karma points MVP 3x admin c-trib
    Jan 07, 2013 @ 20:24
    Dirk De Grave
    101

    Sebastien

    Simply register your events in OnApplicationStarting, just as you did in a ctor of a class deriving from ApplicationBase. Same code, just need to put it in a different place.

    Cheers,

    /Dirk

  • Edwin van Koppen 156 posts 270 karma points
    Jan 18, 2013 @ 11:50
    Edwin van Koppen
    0

    I got this:

    public class AppEvents : IApplicationEventHandler {
            public void OnApplicationInitialized(UmbracoApplication httpApplication, ApplicationContext applicationContext) {

            }
            public void OnApplicationStarting(UmbracoApplication httpApplication, ApplicationContext applicationContext) {
                Document.AfterPublish += DocumentEvents.Document_AfterPublish;
                Document.AfterSave += DocumentEvents.Document_AfterSave;
            }
            public void OnApplicationStarted(UmbracoApplication httpApplication, ApplicationContext applicationContext) {

    }

    }

    but my problem is it runs twice? so you got 2 times the same event.. if i set up a debugger i also got two hits. Dirk don't you have this problem? Have you got a example class you you did this?

  • Sébastien Richer 194 posts 430 karma points
    Jan 22, 2013 @ 23:31
    Sébastien Richer
    0

    Alright Dirk, thanks! Got this working, here's a paste from an answer I gave in another post from my experience with this. I put mine in OnApplicationInitialized, tomorow I'll test for what Edwin is bringing up, I don't think I've ran through these with my debugger, I'll give it a look!

    usingSystem;
    usingSystem.Linq;
    usingUmbraco.Core;
    usingUmbraco.Web;
    using umbraco.BusinessLogic;
    using umbraco.businesslogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;

    namespaceMyNamespace
    {
       
    publicclassNodeHooks:IApplicationEventHandler
       
    {

           
    publicvoidOnApplicationInitialized(UmbracoApplication httpApplication,ApplicationContext applicationContext)
           
    {
               
    Document.New+=DocumentNew;
               
    Document.AfterPublish+=AfterPublish;
           
    }

           
    publicvoidOnApplicationStarting(UmbracoApplication httpApplication,ApplicationContext applicationContext)
           
    {
           
    }

           
    publicvoidOnApplicationStarted(UmbracoApplication httpApplication,ApplicationContext applicationContext)
           
    {
           
    }

           
    staticvoidAfterPublish(Document sender,PublishEventArgs args)
           
    {
               
    if(sender.ContentType.Alias=="OfSomeType")
               
    {
                   
    // Do stuff
               
    }
           
    }

           
    staticvoidDocumentNew(Document sender,NewEventArgs e)
           
    {
               
    if(sender.ContentType.Alias=="OfSomeType")
               
    {
                   
    // Do stuff
               
    }
           
    }

       
    }
    }

    Thanks!

    Seb

  • Edwin van Koppen 156 posts 270 karma points
    Jan 31, 2013 @ 10:54
    Edwin van Koppen
    0

    @Sebastien, does it work with your version? I still got this problem.

  • Sébastien Richer 194 posts 430 karma points
    Jan 31, 2013 @ 13:22
    Sébastien Richer
    0

    Hi Edwin,

    Yes worked like I wanted still havent gotten around to cinfirm events only fire once, but I'm confident. Want to paste you code in here for us to take a look at what might be the problem?

     

    (PS: I'll be away for 4 days, so if I don't write back it's not because I'm mean hah)

  • Edwin van Koppen 156 posts 270 karma points
    Jan 31, 2013 @ 13:24
    Edwin van Koppen
    0

    just look 3 post before this one, i've already posted it.

  • Sébastien Richer 194 posts 430 karma points
    Jan 31, 2013 @ 13:25
    Sébastien Richer
    0

    Well the difference is that you are hooking into publicvoidOnApplicationStartingand me publicvoidOnApplicationInitialized. There is probably a difference there, can you move it to publicvoidOnApplicationInitialized?

  • Edwin van Koppen 156 posts 270 karma points
    Jan 31, 2013 @ 13:31
    Edwin van Koppen
    0

    Doesn't change anything, i've tried already in all the different events but they alle start 2 times.

  • Sébastien Richer 194 posts 430 karma points
    Jan 31, 2013 @ 13:34
    Sébastien Richer
    0

    Hmm... That's quite strange, monday I can try a few things (I'm not at work), I guess this may mean that you application is starting twice? Sorry I can't help more :(

  • Tatiana Vassilieva 18 posts 98 karma points
    May 20, 2013 @ 03:21
    Tatiana Vassilieva
    0

    Is it too late to add question here? Where in the folder structure do you create that class to start with?!

  • Mansoor Ahmad 16 posts 74 karma points
    Nov 07, 2016 @ 13:51
    Mansoor Ahmad
    0

    Just a related question here:

    I have tried both ApplicationEventHandler and IApplicationEventHandler and they are both working fine and same for me.

    My question is which one is better to use ?

Please Sign in or register to post replies

Write your reply to:

Draft