Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ordering of WebActivator calls across multiple assemblies #13

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions TestLibrary/MyStartupCode.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using System;
using System;
using WebActivatorEx;

[assembly: PreApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "Start", RunInDesigner = true)]
[assembly: PreApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "Start2", Order = 2)]
[assembly: PreApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "Start2", Order = 3)]
[assembly: PreApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "Start3", Order = 1)]
[assembly: PostApplicationStartMethod(typeof(TestLibrary.MyStartupCode), "CallMeAfterAppStart")]
[assembly: ApplicationShutdownMethod(typeof(TestLibrary.MyStartupCode), "CallMeWhenAppEnds")]

namespace TestLibrary
{
public static class MyStartupCode
public static class ExecutionLogger
{

public static string ExecutedOrder = "";
}

public static class MyStartupCode
{
public static bool StartCalled { get; set; }
public static bool Start2Called { get; set; }
public static bool CallMeAfterAppStartCalled { get; set; }
Expand All @@ -29,7 +29,7 @@ internal static void Start()
}

StartCalled = true;
ExecutedOrder += "Start";
ExecutionLogger.ExecutedOrder += "Start";
}

public static void Start2()
Expand All @@ -40,12 +40,12 @@ public static void Start2()
}

Start2Called = true;
ExecutedOrder += "Start2";
ExecutionLogger.ExecutedOrder += "Start2";
}

public static void Start3()
{
ExecutedOrder += "Start3";
ExecutionLogger.ExecutedOrder += "Start3";
}

public static void CallMeAfterAppStart()
Expand All @@ -58,7 +58,7 @@ public static void CallMeAfterAppStart()
}

CallMeAfterAppStartCalled = true;
ExecutedOrder += "CallMeAfterAppStart";
ExecutionLogger.ExecutedOrder += "CallMeAfterAppStart";
}

public static void CallMeWhenAppEnds()
Expand All @@ -71,7 +71,7 @@ public static void CallMeWhenAppEnds()
}

CallMeWhenAppEndsCalled = true;
ExecutedOrder += "CallMeWhenAppEnds";
ExecutionLogger.ExecutedOrder += "CallMeWhenAppEnds";
}
}
}
39 changes: 39 additions & 0 deletions TestLibrary2/MyOtherStartupCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

using System;
using TestLibrary;
using WebActivatorEx;

[assembly: PreApplicationStartMethod(typeof(TestLibrary2.MyOtherStartupCode), "Start", Order = 2)]
[assembly: PreApplicationStartMethod(typeof(TestLibrary2.MyOtherStartupCode), "Start2", Order = 4)]


namespace TestLibrary2
{
public static class MyOtherStartupCode
{
public static bool StartCalled { get; set; }
public static bool Start2Called { get; set; }

internal static void Start()
{
if (StartCalled)
{
throw new Exception("Unexpected second call to Start");
}

StartCalled = true;
ExecutionLogger.ExecutedOrder += "OtherStart";
}

public static void Start2()
{
if (Start2Called)
{
throw new Exception("Unexpected second call to Start2");
}

Start2Called = true;
ExecutionLogger.ExecutedOrder += "OtherStart2";
}
}
}
36 changes: 36 additions & 0 deletions TestLibrary2/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TestLibrary2")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TestLibrary2")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("bfe276da-e81c-4dab-8c27-276d085e0a52")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
64 changes: 64 additions & 0 deletions TestLibrary2/TestLibrary2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{638765A7-E010-495F-B325-821D735B2121}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestLibrary2</RootNamespace>
<AssemblyName>TestLibrary2</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MyOtherStartupCode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TestLibrary\TestLibrary.csproj">
<Project>{3D5B026A-2E5F-4C03-8F70-945517215017}</Project>
<Name>TestLibrary</Name>
</ProjectReference>
<ProjectReference Include="..\WebActivator\WebActivator.csproj">
<Project>{339C42C9-C961-4E37-B64E-16F1EF03E4D9}</Project>
<Name>WebActivator</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
14 changes: 13 additions & 1 deletion WebActivator.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# Visual Studio 2010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C2257462-7F5F-4C80-B04B-3D48026B109C}"
ProjectSection(SolutionItems) = preProject
Local.testsettings = Local.testsettings
Expand All @@ -16,6 +16,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestWebApp", "TestWebApp\Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebActivatorTest", "WebActivatorTest\WebActivatorTest.csproj", "{1B623743-BE4E-4034-890F-D378E8C254F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestLibrary2", "TestLibrary2\TestLibrary2.csproj", "{638765A7-E010-495F-B325-821D735B2121}"
EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = WebActivator.vsmdi
Expand Down Expand Up @@ -69,6 +71,16 @@ Global
{1B623743-BE4E-4034-890F-D378E8C254F9}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{1B623743-BE4E-4034-890F-D378E8C254F9}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{1B623743-BE4E-4034-890F-D378E8C254F9}.Release|x86.ActiveCfg = Release|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Debug|Any CPU.Build.0 = Debug|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Debug|x86.ActiveCfg = Debug|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Release|Any CPU.ActiveCfg = Release|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Release|Any CPU.Build.0 = Release|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{638765A7-E010-495F-B325-821D735B2121}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
15 changes: 8 additions & 7 deletions WebActivator/ActivationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,16 @@ public static void RunShutdownMethods()
// Call the relevant activation method from all assemblies
private static void RunActivationMethods<T>(bool designerMode = false) where T : BaseActivationMethodAttribute
{
foreach (var assembly in Assemblies.Concat(AppCodeAssemblies))
var attribs = Assemblies.Concat(AppCodeAssemblies)
.SelectMany(assembly => assembly.GetActivationAttributes<T>())
.OrderBy(att => att.Order);

foreach (var activationAttrib in attribs)
{
foreach (BaseActivationMethodAttribute activationAttrib in assembly.GetActivationAttributes<T>().OrderBy(att => att.Order))
// Don't run it in designer mode, unless the attribute explicitly asks for that
if (!designerMode || activationAttrib.ShouldRunInDesignerMode())
{
// Don't run it in designer mode, unless the attribute explicitly asks for that
if (!designerMode || activationAttrib.ShouldRunInDesignerMode())
{
activationAttrib.InvokeMethod();
}
activationAttrib.InvokeMethod();
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions WebActivatorTest/WebActivatorTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
<Compile Include="WebActivatorUnitTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TestLibrary2\TestLibrary2.csproj">
<Project>{638765A7-E010-495F-B325-821D735B2121}</Project>
<Name>TestLibrary2</Name>
</ProjectReference>
<ProjectReference Include="..\TestLibrary\TestLibrary.csproj">
<Project>{3D5B026A-2E5F-4C03-8F70-945517215017}</Project>
<Name>TestLibrary</Name>
Expand Down
8 changes: 6 additions & 2 deletions WebActivatorTest/WebActivatorUnitTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestLibrary;
using TestLibrary2;
using WebActivatorEx;

namespace WebActivatorTest
Expand All @@ -11,11 +12,14 @@ public class WebActivatorUnitTest
public void TestInit()
{
ActivationManager.Reset();
MyStartupCode.ExecutedOrder = "";
ExecutionLogger.ExecutedOrder = "";
MyStartupCode.StartCalled = false;
MyStartupCode.Start2Called = false;
MyStartupCode.CallMeAfterAppStartCalled = false;
MyStartupCode.CallMeWhenAppEndsCalled = false;

MyOtherStartupCode.StartCalled = false;
MyOtherStartupCode.Start2Called = false;
}

[TestMethod]
Expand Down Expand Up @@ -71,7 +75,7 @@ public void TestWebActivatorMethodsCalledBySpecifiedOrder()
{
ActivationManager.Run();
ActivationManager.RunShutdownMethods();
Assert.AreEqual("StartStart3Start2CallMeAfterAppStartCallMeWhenAppEnds", MyStartupCode.ExecutedOrder);
Assert.AreEqual("StartStart3OtherStartStart2OtherStart2CallMeAfterAppStartCallMeWhenAppEnds", ExecutionLogger.ExecutedOrder);
}
}
}
Binary file not shown.