Try our conversational search powered by Generative AI!

Initialization module initializes just sometimes

Vote:
 

My initialization module behaves a bit strangely, it seems to initialize at random. Sometimes it's run 4, 5, 6 times in a row, and then it just won't initialize.
This happens in the development environment as well as in stage. I've set break points in the Initialize method and they're sometimes not hit.

What can be wrong? Here's the code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework;
using IAR.Base.PageTypeDefinitions;
using PageTypeBuilder;

namespace MyProject.Base.EventHandlers
{
	[InitializableModule]
	[ModuleDependency(typeof(PageTypeBuilder.Initializer))]
	public class RedirectHandler : IInitializableModule
	{

		private bool _pageEventAttached;

		/// <summary>
		/// Attaches event handlers
		/// </summary>
		/// <param name="context">The context.</param>
		public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
		{

			if (!_pageEventAttached)
			{
				PageBase.PageSetup += new PageSetupEventHandler(PageSetup); 
			}
			_pageEventAttached = true;
		}

		private void PageSetup(PageBase sender, PageSetupEventArgs e)
		{
			sender.Init += new EventHandler(My_Method);
		}

		private void My_Method(object sender, EventArgs e)
		{
			// ...
		}

		/// <summary>
		/// Preload is not required for this module
		/// </summary>
		/// <param name="parameters">The parameters.</param>
		public void Preload(string[] parameters)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// Uninitializes the specified context.
		/// </summary>
		/// <param name="context">The context.</param>
		public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
		{
			_pageEventAttached = false;
		}
	}
}

    

http://world.episerver.com/Modules/Forum/Pages/Forum.aspx?id=46471&epslanguage=en
#55567
Dec 07, 2011 17:34
Vote:
 

Don't know if this solves you problem, but you should remove your events aswell and move your _pageEventAttached into the if-statement.

if (!_pageEventAttached)
{
     PageBase.PageSetup += new PageSetupEventHandler(PageSetup);
     _pageEventAttached = true;
}
public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
{
     PageBase.PageSetup -= new PageSetupEventHandler(PageSetup);
     _pageEventAttached = false;
}

  

Otherwise I guees you could end up with multiple events.

#55571
Dec 07, 2011 18:24
Vote:
 
Hi Martin, This blog post might help you solve your debugging problem http://world.episerver.com/Blogs/Magnus-Strale/Dates/2010/8/Debugging-initialization-modules/.
#55579
Dec 08, 2011 10:03
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.