Skip to content

Instantly share code, notes, and snippets.

@jbrinkman
Created June 27, 2013 21:50
Show Gist options
  • Save jbrinkman/5880720 to your computer and use it in GitHub Desktop.
Save jbrinkman/5880720 to your computer and use it in GitHub Desktop.
This is a simple base implementation for HTTPModules to inherit from. It exposes the application start and module initialization methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DotNetNuke.Components
{
public abstract class HttpModuleBase : IHttpModule
{
#region Static privates
private static volatile bool applicationStarted = false;
private static object applicationStartLock = new object();
#endregion
#region IHttpModule implementation
/// <summary>
/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
/// </summary>
public void Dispose()
{
// dispose any resources if needed
}
/// <summary>
/// Initializes the specified module.
/// </summary>
/// <param name="context">The application context that instantiated and will be running this module.</param>
public void Init(HttpApplication context)
{
if (!applicationStarted)
{
lock (applicationStartLock)
{
if (!applicationStarted)
{
// this will run only once per application start
this.OnStart(context);
applicationStarted = true;
}
}
}
// this will run on every HttpApplication initialization in the application pool
this.OnInit(context);
}
#endregion
/// <summary>Initializes any data/resources on application start.</summary>
/// <param name="context">The application context that instantiated and will be running this module.</param>
public virtual void OnStart(HttpApplication context)
{
// put your application start code here
}
/// <summary>Initializes any data/resources on HTTP module start.</summary>
/// <param name="context">The application context that instantiated and will be running this module.</param>
public virtual void OnInit(HttpApplication context)
{
// put your module initialization code here
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment