-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ApplicationShutdownMethod support + cleanup
- Loading branch information
davidebbo
committed
Mar 1, 2011
1 parent
c1b8c86
commit 586b23d
Showing
12 changed files
with
122 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,53 @@ | ||
using System.Web.Mvc; | ||
using System.Web.Routing; | ||
using System; | ||
|
||
[assembly: WebActivator.PreApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "Start")] | ||
[assembly: WebActivator.PreApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "Start2")] | ||
[assembly: WebActivator.PostApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "CallMeAfterAppStart")] | ||
[assembly: WebActivator.ApplicationShutdownMethod(typeof(TestLibrary.MyStartupCode), "CallMeWhenAppEnds")] | ||
|
||
namespace TestLibrary { | ||
public static class MyStartupCode { | ||
public static bool StartCalled { get; set; } | ||
public static bool Start2Called { get; set; } | ||
public static bool CallMeAfterAppStartCalled { get; set; } | ||
public static bool CallMeWhenAppEndsCalled { get; set; } | ||
|
||
internal static void Start() { | ||
if (StartCalled) { | ||
throw new Exception("Unexpected second call to Start"); | ||
} | ||
|
||
StartCalled = true; | ||
} | ||
|
||
public static void Start2() { | ||
if (Start2Called) { | ||
throw new Exception("Unexpected second call to Start2"); | ||
} | ||
|
||
Start2Called = true; | ||
} | ||
|
||
public static void CallMeAfterAppStart() { | ||
// This gets called after global.asax's Application_Start | ||
|
||
if (CallMeAfterAppStartCalled) { | ||
throw new Exception("Unexpected second call to CallMeAfterAppStart"); | ||
} | ||
|
||
CallMeAfterAppStartCalled = true; | ||
} | ||
|
||
public static void CallMeWhenAppEnds() { | ||
// This gets called when the app shuts down | ||
|
||
if (CallMeWhenAppEndsCalled) { | ||
throw new Exception("Unexpected second call to CallMeWhenAppEnds"); | ||
} | ||
|
||
CallMeWhenAppEndsCalled = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace WebActivator { | ||
// Same as PreApplicationStartMethodAttribute, but for methods to be called when the app shuts down | ||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] | ||
public sealed class ApplicationShutdownMethodAttribute : BaseActivationMethodAttribute { | ||
public ApplicationShutdownMethodAttribute(Type type, string methodName) | ||
: base(type, methodName) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Reflection; | ||
|
||
namespace WebActivator { | ||
static class AssemblyExtensions { | ||
public static IEnumerable<PreApplicationStartMethodAttribute> GetPreAppStartAttributes(this Assembly assembly) { | ||
// Go through all the PreApplicationStartMethodAttribute attributes | ||
// Note that this is *our* attribute, not the System.Web namesake | ||
|
||
return assembly.GetCustomAttributes( | ||
typeof(PreApplicationStartMethodAttribute), | ||
inherit: false).OfType<PreApplicationStartMethodAttribute>(); | ||
} | ||
|
||
public static IEnumerable<PostApplicationStartMethodAttribute> GetPostAppStartAttributes(this Assembly assembly) { | ||
// Return all the attributes of a given type from an assembly | ||
public static IEnumerable<T> GetActivationAttributes<T>(this Assembly assembly) where T : BaseActivationMethodAttribute { | ||
return assembly.GetCustomAttributes( | ||
typeof(PostApplicationStartMethodAttribute), | ||
inherit: false).OfType<PostApplicationStartMethodAttribute>(); | ||
typeof(T), | ||
inherit: false).OfType<T>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters