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
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);
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.
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.
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
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.
thanks !! saved my day !! :)
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.
is working on a reply...