SIMULATE "APPLICATION_START" IN CLASS LIBRARY

In the past, if you needed to run startup code in a class library, you included a method call in the "Application_Start" of the Global.asax file. Thankfully, .NET 4.0 has made things easier with the PreApplicationStartMethodAttribute! This helpful attribute allows us to hand-pick code to execute when the application starts without ever modifying the Global.asax file, or messier yet, creating an HttpHandler.

Let's see it in action!

Old & Busted

First, here's a refresher of the old way. Say we have a chunk of code in our library that needs to be executed when the application starts:

namespace ClassLibrary
{
    public class Startup
    {
        public static void Start()
        {
            // do some awesome stuff here!
        }
    }
}

In the past, we had to manually add a reference to this in the "Application_Start" of our Global.asax file:

protected void Application_Start(object sender, EventArgs e)
{
	ClassLibrary.Startup.Start();
}
New Hotness

Now that we live in the future, all we have to do is add a PreApplicationStartMethod assembly attribute (which is a part of the System.Web namespace) to our class library with parameters specifying our class and method to be executed:

[assembly: PreApplicationStartMethod(typeof(ClassLibrary.Startup), "Start")]

You can add this assembly attribute anywhere, but I find it best to either include it in the AssemblyInfo.cs file, or at the beginning of the class containing the code to be executed. Here's the Startup class in its entirety to demonstrate my meaning:

using System.Web;

[assembly: PreApplicationStartMethod(typeof(ClassLibrary.Startup), "Start")]

namespace ClassLibrary
{
    public class Startup
    {
        public static void Start()
        {
            // do some awesome stuff here!
        }
    }
}

It's as simple as that! The only downside is you need to be using the .NET Framework 4.0 or later (which is why I was not able to use this for a previous Sharepoint solution). But, if you are using 4.0 or later, this is a great way to keep your library code separate from your web project.

Notes and Such

It's worth mentioning that the behaviour of the PreApplicationStartMethodAttribute is a little different between .NET Frameworks 4.0 and 4.5. In 4.0, "AllowMultiple" is set to "false", so you can only include this attribute ONCE in your library - so all your code must be within one method. However, in 4.5, "AllowMultiple" is set to "true", so you may include as many startup methods as your heart desires!

Shout-outs

Lastly, I'd be remiss if I didn't mention WebActivator. It's an excellent library that leverages the PreApplicationStartMethodAttribute but adds a little more oomph and versatility (including the ability to have startup and shutdown code!).

Now go and code!

Simulate "Application_Start" in Class Library
12.30.2013 ~ 12:37 AM
posted in professional
tagged in asp-net : c#
comments [2]

 
 
 
 
@ Ryan
Dude, I have no idea what any of this means. But I like how the words make me feel inside. Jittery.
1.03.2014 - 08:30 PM
 
 
@ Zachary
Cool! Is there something like it for a normal class library. I'm running MSTests, and a class with the above mention attribute doesn't fire. Any time an assembly is referenced, or when MsTest starts up.
5.19.2014 - 04:11 PM
 
 

SPEAK!

Please enter your comment below.