Commits

Jakub Konecki committed f34608f

Added support for executing WebActivator outside ASP.NET runtime.

  • Participants
  • Parent commits 2edcfa5

Comments (0)

Files changed (1)

File WebActivator/PreApplicationStartCode.cs

-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
 using System.Web;
+using System.Web.Hosting;
 
 namespace WebActivator {
     public class PreApplicationStartCode {
-        static List<PreApplicationStartMethodAttribute> _attribsToCallAfterStart = new List<PreApplicationStartMethodAttribute>();
+        static List<PreApplicationStartMethodAttribute> attribsToCallAfterStart = new List<PreApplicationStartMethodAttribute>();
+        private static object initLock = new object();
+        private static bool hasInited;
 
         public static void Start() {
-            // Go through all the bin assemblies
-            foreach (var assemblyFile in Directory.GetFiles(HttpRuntime.BinDirectory, "*.dll")) {
-                var assembly = Assembly.LoadFrom(assemblyFile);
+            lock (initLock) {
+                if (!hasInited) {
+                    // Go through all the bin assemblies
+                    foreach (var assemblyFile in GetAssemblyFiles()) {
+                        var assembly = Assembly.LoadFrom(assemblyFile);
 
-                // Go through all the PreApplicationStartMethodAttribute attributes
-                // Note that this is *our* attribute, not the System.Web namesake
-                foreach (PreApplicationStartMethodAttribute preStartAttrib in assembly.GetCustomAttributes(
-                    typeof(PreApplicationStartMethodAttribute),
-                    inherit: false)) {
+                        // Go through all the PreApplicationStartMethodAttribute attributes
+                        // Note that this is *our* attribute, not the System.Web namesake
+                        foreach (PreApplicationStartMethodAttribute preStartAttrib in assembly.GetCustomAttributes(
+                            typeof(PreApplicationStartMethodAttribute),
+                            inherit: false)) {
 
-                    // If it asks to be called after global.asax App_Start, keep track of the method. Otherwise call it now
-                    if (preStartAttrib.CallAfterGlobalAppStart) {
-                        _attribsToCallAfterStart.Add(preStartAttrib);
+                            // If it asks to be called after global.asax App_Start, keep track of the method. Otherwise call it now
+                            if (preStartAttrib.CallAfterGlobalAppStart && HostingEnvironment.IsHosted) {
+                                attribsToCallAfterStart.Add(preStartAttrib);
+                            }
+                            else {
+                                // Invoke the method that the attribute points to
+                                preStartAttrib.InvokeMethod();
+                            }
+                        }
                     }
-                    else {
-                        // Invoke the method that the attribute points to
-                        preStartAttrib.InvokeMethod();
+
+                    // If any method needs to be called later, register our module to do it
+                    if (attribsToCallAfterStart.Count > 0) {
+                        Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(StartMethodCallingModule));
                     }
+
+                    hasInited = true;
                 }
             }
+        }
 
-            // If any method needs to be called later, register our module to do it
-            if (_attribsToCallAfterStart.Count > 0) {
-                Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(StartMethodCallingModule));
-            }
+        private static IEnumerable<string> GetAssemblyFiles() {
+            string directory = HostingEnvironment.IsHosted ? HttpRuntime.BinDirectory : Environment.CurrentDirectory;
+            return Directory.GetFiles(directory, "*.dll");
         }
 
         class StartMethodCallingModule : IHttpModule {
                 // Make sure we only call the methods once per app domain
                 lock (initLock) {
                     if (!hasInited) {
-                        _attribsToCallAfterStart.ForEach(a => a.InvokeMethod());
+                        attribsToCallAfterStart.ForEach(a => a.InvokeMethod());
                         hasInited = true;
                     }
                 }
             }
         }
     }
-}
+}