Hooking into application_start

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
2/27/2013 8:12:03 AM
Gravatar
Total Posts 19

Hooking into application_start

Hi Joe

I've been trying to add some code that will get run at [or just before] the application start event.  As I can't edit the global.asax I was trying to use WebActivator to do the work for me, it working fine it my test app but not at all in a mojoPortal deployment [2.9.5]. So I tried to use the .net PreApplicationStartMethod instead, this didn't work either.  I was using the WebActivator in preference as the .net method only allows you to use that attribute once per assembly.

Are you aware of anything that may prevent this from working in a mojoPortal site?  I've checked that the correct dll's were in the bin and the site was running under .net 4.

Another reason for wanting to do this is that it would be nice to do dynamic module registration instead of having to edit the web.config every time we deploy/upgrade - http://blog.davidebbo.com/2011/02/register-your-http-modules-at-runtime.html

I appreciate that it's not strictly a mojoPortal question but I would be interested to hear your views on how best to hook into the application start event.

Many thanks.

Stewart.

 

 

2/28/2013 12:39:39 PM
Gravatar
Total Posts 18439

Re: Hooking into application_start

I don't think that is going to work because I think those examples where he shows assembly references would have to be coded into mojoPortal and therefore known ahead of time.

ie [assembly: WebActivator.PostApplicationStartMethod(...

So it is not really a way to extend an existing app without code modifications, it can only be used in an app you are coding yourself or by modifying the mojoPortal code.

Since you should not fork the mojoPortal code the best way to add custom http modules is via web.config. Upgrading is a relatively infrequent activity in the grand scheme of things so I don't see maintaining web.config customizations as a big deal.

There really is no perfect way to hook into the application_start event since that really only fires in global.asax.cs of which there can only be one and one already exists in mojoPortal. However you can sort of mimic the app start in an HttpModule with code like this:

using System;
using System.Web;

namespace yournamespace
{
    public class AppStartHttpModule : IHttpModule
    {
        private static bool HasAppStarted = false;
        private readonly static object _syncObject = new object();

        public void Init(HttpApplication application)
        {
            
            if (!HasAppStarted)
            {
                lock (_syncObject)
                {
                    if (!HasAppStarted)
                    {
                        DoSomething();
                        HasAppStarted = true;
                    }
                }
            }
            
        }

        private void DoSomething()
        {
            

        }

        public void Dispose() { }
    }
}

Hope that helps,

Joe

3/1/2013 2:37:02 AM
Gravatar
Total Posts 19

Re: Hooking into application_start

Hi Joe

Thanks for the detailed reply.

Having reinstalled my mojoPortal site I can happily confirm that the WebActivator does indeed work (it worked when I deployed to the server so I assumed there was something going on with my local installation).

This now provides a simple way to tap into pre/post application start.

[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(esdmMojoPortal.SHINE.UI.ShineBackgroundTasks), "Start")]  

  Public static class ShineBackgroundTasks    
  {        
    public static void Start()       
     {                       
        ShineUILogger.LogInfo("Shine background tasks - start");                                   
         var task = IntervalTask.CreateTask(context => {
         //send notification emails    

Many thanks

Stewart

 

You must sign in to post in the forums. This thread is closed to new posts.