Commits

Mihai Mogosanu committed c84b4b2

Added support for executing methods in the desired order.
BaseActivationMethodAttribute has the Order property
Added relevant tests

Comments (0)

Files changed (7)

TestLibrary/MyStartupCode.cs

-using System.Web.Mvc;
+using System.Collections.Generic;
+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.PreApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "Start2",Order = 2)]
+[assembly: WebActivator.PreApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "Start3",Order = 1)]
 [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 string ExecutedOrder="";
+		public static bool StartCalled { get; set; }
         public static bool Start2Called { get; set; }
         public static bool CallMeAfterAppStartCalled { get; set; }
         public static bool CallMeWhenAppEndsCalled { get; set; }
             }
 
             StartCalled = true;
+        	ExecutedOrder += "Start";
         }
 
         public static void Start2() {
             }
 
             Start2Called = true;
+			ExecutedOrder += "Start2";
         }
 
+		public static void Start3()
+		{
+			ExecutedOrder += "Start3";
+		}
+
         public static void CallMeAfterAppStart() {
             // This gets called after global.asax's Application_Start
 
             }
 
             CallMeAfterAppStartCalled = true;
+			ExecutedOrder += "CallMeAfterAppStart";
         }
 
         public static void CallMeWhenAppEnds() {
                 throw new Exception("Unexpected second call to CallMeWhenAppEnds");
             }
 
-            CallMeWhenAppEndsCalled = true;
+            CallMeWhenAppEndsCalled = true;			
+			ExecutedOrder += "CallMeWhenAppEnds";
         }
     }
 }

TestWebApp/TestStartupCode/MyStartupCode.cs

-using System.Web.Mvc;
+using System.Diagnostics;
+using System.Web.Mvc;
 using System.Web.Routing;
 
-[assembly: WebActivator.PreApplicationStartMethod(typeof(TestWebApp.TestStartupCode.MyStartupCode), "Start")]
+[assembly: WebActivator.PreApplicationStartMethod(typeof(TestWebApp.TestStartupCode.MyStartupCode), "Start",Order = 1)]
 
 namespace TestWebApp.TestStartupCode {
     public static class MyStartupCode {
 
         public static void Start() {
             StartCalled = true;
+			Debug.WriteLine("MyStartUpCode1");			
         }
     }
 }

TestWebApp/TestStartupCode/MyStartupCode2.cs

+using System.Diagnostics;
+using System.Web.Mvc;
+using System.Web.Routing;
+
+[assembly: WebActivator.PreApplicationStartMethod(typeof(TestWebApp.TestStartupCode.MyStartupCode2), "Start",Order = 0)]
+
+namespace TestWebApp.TestStartupCode {
+    public static class MyStartupCode2 {
+        public static bool StartCalled { get; set; }
+
+        public static void Start() {
+            StartCalled = true;
+			Debug.WriteLine("MyStartUpCode2");
+        }
+    }
+}

TestWebApp/TestWebApp.csproj

     </Compile>
     <Compile Include="Models\AccountModels.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="TestStartupCode\MyStartupCode2.cs" />
     <Compile Include="TestStartupCode\MyStartupCode.cs" />
   </ItemGroup>
   <ItemGroup>

WebActivator/ActivationManager.cs

         // Call the relevant activation method from all assemblies
         private static void RunActivationMethods<T>() where T : BaseActivationMethodAttribute {
             foreach (var assembly in Assemblies.Concat(AppCodeAssemblies)) {
-                foreach (BaseActivationMethodAttribute activationAttrib in assembly.GetActivationAttributes<T>()) {
+                foreach (BaseActivationMethodAttribute activationAttrib in assembly.GetActivationAttributes<T>().OrderBy(att=>att.Order)) {
                     activationAttrib.InvokeMethod();
                 }
             }

WebActivator/BaseActivationMethodAttribute.cs

             }
         }
 
+		public byte Order { get; set; }
+
+
         public void InvokeMethod() {
             // Get the method
             MethodInfo method = Type.GetMethod(

WebActivatorTest/WebActivatorUnitTest.cs

 
             Assert.IsTrue(MyStartupCode.CallMeWhenAppEndsCalled);
         }
+
+		[TestMethod]
+		public void  TestWebActivatorMethodsCalledBySpecifiedOrder()
+		{
+			WebActivator.ActivationManager.Run();
+			WebActivator.ActivationManager.RunShutdownMethods();
+			Assert.AreEqual("StartStart3Start2CallMeAfterAppStartCallMeWhenAppEnds", MyStartupCode.ExecutedOrder);
+		}
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.