Copied to clipboard

Flag this post as spam?

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


  • M 33 posts 91 karma points
    Jul 11, 2013 @ 15:32
    M
    0

    ApplicationEventHandler - ApplicationStarted is fired twice

    I've a website using Umbraco 6.1.1 with uBlogsy 3.0. On Publishing any node the publish event is fired twice. It's very risky for our application as we send emails to client on publishing a node. Below is the code showing how events are connected.

    namespace Umbraco.Extensions.EventHandlers
    {
        public class RegisterEvents : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                Document.BeforePublish += Document_BeforePublish;
                Document.AfterPublish += Document_AfterPublish;
            }
            private void Document_AfterPublish(Document sender, PublishEventArgs e)
            {
                //logic after node publish - reached twice
            }
        }
    }
  • Jamie Pollock 174 posts 853 karma points c-trib
    Jul 11, 2013 @ 18:14
    Jamie Pollock
    0

    Hi M,
    Have you considered using a lock as a work around for assigning the events?

    See the following wiki entry: http://our.umbraco.org/wiki/reference/api-cheatsheet/using-iapplicationeventhandler-to-register-events

    I've copied the snippet over in case it changes:

    private static object _lockObj = new object();
    private static bool _ran =false;

    public void OnApplicationStarted(UmbracoApplication httpApplication,Umbraco.Core.ApplicationContext applicationContext) {
    // lock
    if (!_ran) {
    lock (_lockObj) {
    if (!_ran) {
    // everything we do here is blocking
    // on application start, so we should be
    // quick.
    // do you're registering here... or in a function
    // you will need to add the relevent class at the top of your code (i.e using Umbraco.cms.businesslogic.web)

    Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);

    _ran = true;


    }
    }

     

    Jamie

  • M 33 posts 91 karma points
    Oct 30, 2013 @ 07:43
    M
    102

    Grrr...This was infact silly! This umbraco 6 website is created in visual studio MVC. The .cs file that handled events registrations needed to be in "Content" mode instead of "Compile" (in file properties) as it resided in App_Code. 

  • Balbir singh 9 posts 102 karma points
    Apr 25, 2016 @ 08:38
    Balbir singh
    1

    thanks !! saved my day !! :)

  • Daniel Bardi 927 posts 2562 karma points
    Mar 13, 2014 @ 17:10
    Daniel Bardi
    0

    This is old, but I would recommend that you setup event assignments in the constructor instead of ApplicationStarted().

    Only use ApplicationStarted() if you need to access the appliction or context.

Please Sign in or register to post replies

Write your reply to:

Draft