
Running Module Code at Application Start in DNN
Occasionally 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:
public void RegisterRoutes(IMapRoute mapRouteManager) { ... }
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:
private static string InitializeApp(HttpApplication app, ref bool initialized)
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
Sebastian Leupold
there is a small mistake, it should be “(formerly DotNetNuke in the pre re-branding era)” 🙂
Brian Dukes
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).