Running Module Code at Application Start in DNN

running a module code

Running Module Code at Application Start in DNN

DNNCorp_logo_lrgOccasionally you will find that you will want to wire up some methods to run when the DNN (formerly DotNetNuke in the post re-branding era) application starts up.

How to Run Module Code at Application Start in DNN?

There are a number of reasons you may want to do this but for many its likely for performance, caching, monitoring or a service endpoint.

The last one I mentioned is interesting and is the crux for making this whole thing work. Before I dive into the interesting bits I will mention that if you control your DNN installation and are not a module developer looking to have others install your module to get utilize this benefit then you could simply add your function handlers to the global.ascx and be good to go.

Back to the magic at hand you can alternatively utilize the new Web API service framework built into DNN  in order to get your code to launch at application start. Implementing the IServiceRouteMapper interface on any of your classes will take care of this for you.

Within the the following method implementation you can wire up your code:

[code lang=”csharp”]
public void RegisterRoutes(IMapRoute mapRouteManager)
{

}
[/code]

What makes this special is that DNN already searches for any classes that implements the IServiceRouteMapper in order to wire up the Web API service endpoints.  As long as you don’t modify the mapRouteManager parameter then the Web API will continue to work as intended and you can implement any custom code you desire.

You can search for:

[code lang=”csharp”]private static string InitializeApp(HttpApplication app, ref bool initialized)[/code]

in the Initialize.cs for when in the timeline ServicesRoutingManager.RegisterServiceRoutes() gets fired.

You can check out my repository for File Monitor on how and why I found this useful in the FileMonitor.cs

Antonio Chagoury

Hi, I'm Antonio, Founder and CEO of Maxiom Technology (formerly Inspector IT). I'm a technology executive and entrepreneur who has achieved consistent success in driving growth, generating revenue, and enhancing value in domestic and international markets through technology product innovations.

2 Comments
  • Sebastian Leupold
    Posted at 10:46 am, July 18, 2013

    there is a small mistake, it should be “(formerly DotNetNuke in the pre re-branding era)” 🙂

  • Posted at 12:08 pm, July 18, 2013

    While adding events in the global.asax is an option, just remember that you’ll have to put them back after every DNN upgrade (and those who follow you in the maintenance of the site will have to know to do that, as well).