Clone wiki

WebActivator / Home


This project has been moved to!

WebActivator is a NuGet package that allows other packages to easily bring in Startup and Shutdown code into a web application. This gives a much cleaner solution than having to modify global.asax with the startup logic from many packages.

Integrating WebActivator into your package

Step 1: Add a dependency on it in your package's nuspec file

<?xml version="1.0"?>
<package xmlns="">
      <dependency id="WebActivator" version="1.4" />

The only thing that needs to be added for WebActivator is the <dependency> line.

Step 2: Add a preprocessed source file in Content\App_Start

Under the folder where your nuspec file is, create a Content\App_Start folder. In there, create a file named MySuperPackage.cs.pp:

using System;

[assembly: WebActivator.PreApplicationStartMethod(
    typeof($rootnamespace$.App_Start.MySuperPackage), "PreStart")]

namespace $rootnamespace$.App_Start {
    public static class MySuperPackage {
        public static void PreStart() {
            // Add your start logic here

And that's it! Now your code will be called early on when the app starts.

Choosing when your code executes

The example above uses the PreApplicationStartMethod attribute, which makes the code run very early on, before global.asax's Application_Start gets too run.

If that's too early for you, you can instead use the PostApplicationStartMethod attribute, which gets your code called after global.asax's Application_Start.

And if you have some logic you'd like to execute when the app shuts down, you can use the ApplicationShutdownMethod attribute.

Other than their name changing, all three attributes are used the same way.

Please follow the folder convention

As you saw in the example above, I placed the .cs.pp file under the App_Start folder, such that when someone installs your package, the .cs file ends up in the App_Start folder under the app root. Technically, WebActivator does not require this, but it's a good convention to follow so that the startup code from all the packages all ends up in one folder rather than being at the root of the app (which we don't want to pollute).

The picky ones will bring up that App_Start is not a great name when you use WebActivator for shut down logic. Maybe so, but since shut down logic is somewhat rare, and often goes along with startup logic, it's best to keep everything under App_Start and live with this small naming anomaly :)

Support for Web Sites

In a Web Site (as opposed to a Web Application), you typically put your shared code in the App_Code folder. If you have code in there that uses the PostApplicationStartMethod attribute, it will get called when the app starts, giving Web Sites some WebActivator love.

Please note that you can only use PostApplicationStartMethod in App_Code, and not PreApplicationStartMethod. The reason is that when PreApplicationStartMethod fires, the App_Code folder has not even been compiled!

Support for invoking the start methods outside of ASP.NET

This change came courtesy of Jakub Konecki, who needed it for unit testing purpose. This comes as a set of static methods that you can use to invoke the startup methods:

// Run all the WebActivator PreStart methods
// Run all the WebActivator PostStart methods
// Run all the WebActivator start methods
// Run all the WebActivator shutdown methods

Note that normally you would not call these methods explicitly from a web application. But if you are using some 'WebActivated' NuGet packages from a different type of apps (say a Console app), it could make sense to have add calls to Run() and RunShutdownMethods() at the beginning and end of your app.

Change history

1.5.1 (5-31-2012)

  • Fixed bug that happened when there are multiple overloads of the method (Start, ...). Always pick the parameterless overload.

1.5 (10-26-2011)

  • Added ability to order method invocations within one assembly

1.4.3 (9-14-2011)

  • Take dependency on Microsoft.Web.Infrastructure

1.4 (2-28-2011)

  • Added ability to run code when the app shuts down

1.3 (2-24-2011)

  • Added support for Web Sites

1.2 (2-21-2011)

  • Added support for executing WebActivator outside ASP.NET runtime

1.1 (2-15-2011)

  • Added support for methods called after global.asax App_Start

1.0 (10-8-2010)

  • Initial release

Related blog posts

Light up your NuGets with startup code and WebActivator

New features in WebActivator 1.4

App_Start folder convention for NuGet and WebActivator