private void ShutDown() { try { lock (SyncRoot) { Trace.WriteLine("inside shutting down host"); s_current = null; HttpRuntime.Close(); HttpRuntime.UnloadAppDomain(); } } catch (Exception ex) { Trace.WriteLine("Exception inside unloading HttpRuntime host:" + ex); } }
/// <summary> /// Unload the passed Host's AppDomain. /// </summary> internal static void ShutDown(AspFixtureHost host) { if (AspFixtureHost.Current != null) { throw new InvalidOperationException("Cannot shutdown the Host from within the Host's AppDomain"); } try { s_current = null; host.ShutDown(); } catch (Exception ex) { Trace.WriteLine("Exception during fixture host shutdown:" + ex); } }
/// <summary> /// Initializes the newly created host instance. /// </summary> private void Initialize(string rootLocation, AppDomain creatorDomain, TextWriter cout, string[] preloadAssemblies) { if (AspFixtureHost.Current != null) { throw new InvalidOperationException("Cannot initialize the Host from within the Host's AppDomain"); } _preloadedAssemblies = new Assembly[preloadAssemblies.Length]; for (int i = 0; i < _preloadedAssemblies.Length; i++) { _preloadedAssemblies[i] = Assembly.LoadFrom(preloadAssemblies[i]); } AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); // "redirect" http protocol RegisterAspTestPseudoProtocol(); // remember rootLocation _rootLocation = rootLocation; // remember creating AppDomain _creatorDomain = creatorDomain; // redirect cout to the passed TextWriter Console.SetOut(cout); // Make this Host instance available through AspFixtureHost.Current s_current = this; // force HttpRuntime initialization StringWriter sw = new StringWriter(); AspFixtureSimpleWorkerRequest wr = new AspFixtureSimpleWorkerRequest(string.Empty, string.Empty, sw); HttpRuntime.ProcessRequest(wr); if (wr.StatusCode != 200 && wr.StatusCode != 404) { throw new Exception("HttpRuntime Setup Failure:" + sw.ToString()); } }
/// <summary> /// Creates a new Host instance within a new AppDomain based on the passed in Properties. /// </summary> /// <remarks> /// You must not call this method from within an existing Host's AppDomain. /// </remarks> private static AspFixtureHost CreateInstance(string virtualPath, string relativePhysicalPath, string rootLocation) { if (AspFixtureHost.Current != null) { throw new InvalidOperationException("Cannot create a new Host within an existing host"); } string currentDir = new FileInfo(new Uri(rootLocation).LocalPath).DirectoryName; //AppDomain.CurrentDomain.BaseDirectory; // setup up target directory string physicalHostDir = currentDir.TrimEnd('\\', '/') + "\\" + relativePhysicalPath.Trim('\\', '/') + "\\"; physicalHostDir = new DirectoryInfo(physicalHostDir).FullName.TrimEnd('\\') + "\\"; Trace.WriteLine("Creating AspFixtureHost instance at " + physicalHostDir); string physicalHostBinDir = physicalHostDir + "bin\\"; // copy all files from current build output directory to <webroot>/bin Directory.CreateDirectory(physicalHostBinDir); foreach (string file in Directory.GetFiles(currentDir, "*.*")) { if ( (file.ToLower().IndexOf("nunit.core.") > -1) //|| file.ToLower().IndexOf("nunit.framework.") > -1 ) { continue; } string newFile = Path.Combine(physicalHostBinDir, Path.GetFileName(file)); if (File.Exists(newFile)) { File.Delete(newFile); } File.Copy(file, newFile); } // copy framework specific web.config file string webConfigFile = string.Format("{0}\\web.config.net-{1}", physicalHostDir, Environment.Version.ToString(2)); if (File.Exists(webConfigFile)) { string defaultConfigFile = string.Format("{0}\\web.config", physicalHostDir); if (File.Exists(defaultConfigFile)) { File.Delete(defaultConfigFile); } File.Copy(webConfigFile, defaultConfigFile); } // finally create & initialize Web Application Host instance virtualPath = "/" + virtualPath.Trim('/'); AspFixtureHost _host = (AspFixtureHost)System.Web.Hosting.ApplicationHost.CreateApplicationHost(typeof(AspFixtureHost), virtualPath, physicalHostDir); string[] preloadAssemblies = new string[] { // typeof(TestAttribute).Assembly.Location // , typeof(ITest).Assembly.Location // nunit.core.interfaces // , typeof(NUnit.Core.CoreExtensions).Assembly.Location // nunit.core }; _host.Initialize(currentDir, AppDomain.CurrentDomain, Console.Out, preloadAssemblies); AspFixtureRequest.AspFixtureRequestFactory factory = AspFixtureRequest.Factory; WebRequest.RegisterPrefix("asptest", factory); factory.Host = _host; return(_host); }