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.
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>MySuperPackage</id>
etc...
<dependencies>
<dependency id="WebActivatorEx" version="2.0.0" />
</dependencies>
</metadata>
</package>
The only thing that needs to be added for WebActivator is the <dependency>
line.
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: WebActivatorEx.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.
The example above uses the PreApplicationStartMethod attribute, which makes the code run very early on, before global.asax's Application_Start gets to 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.
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 :)
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!
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
WebActivator.ActivationManager.RunPreStartMethods();
// Run all the WebActivator PostStart methods
WebActivator.ActivationManager.RunPostStartMethods();
// Run all the WebActivator start methods
WebActivator.ActivationManager.Run();
// Run all the WebActivator shutdown methods
WebActivator.ActivationManager.RunShutdownMethods();
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.
- Add support for
webactivator:assembliesToScan
App Setting to only scan a fixed list.
e.g.
<add key="webactivator:assembliesToScan" value="Foo,Bar" />
This overrides the excludedFilesExpression flag
- Add support for
webactivator:excludedFilesExpression
App Setting. #28
- Re-release under Apache 2 license
- Use Retail instead of Debug assembly in NuGet package
- Fixed to work on Mono #15
- Make the Order parameter work across multiple assemblies #13
- Ignore GetCustomAttributes exceptions to work around #12
- The WebActivator assembly is now signed (and is now named WebActivatorEx.dll)
Change contributed by @gdoten
- Look in CodeBase rather than Location directory for assemblies to scan for attirbutes.
- Make Assembly.LoadFrom exception handling a little more specific.
- Fix to unit testing ExecuteOrder when all test are run at the same time.
- Only difference with 1.5.1 is the corrected project site link so it points to github instead of the old bitbuck
- Rerelease after moving from bitbucket to github
- Added ability to order method invocations within one assembly
- Take dependency on Microsoft.Web.Infrastructure
- Added ability to run code when the app shuts down
- Added support for Web Sites
- Added support for executing WebActivator outside ASP.NET runtime
- Added support for methods called after global.asax App_Start
- Initial release
Light up your NuGets with startup code and WebActivator