Files
Orchard/src/Orchard.Specs/Hosting/WebHost.cs
Renaud Paquay 01c7e29db2 Fix unit test memory leak
Shutdown ASP.NET AppDomains after running each SpecFlow scenario.

--HG--
branch : dev
2010-05-22 17:50:18 -07:00

84 lines
3.2 KiB
C#

using System;
using System.IO;
using System.Reflection;
using System.Web.Hosting;
using Orchard.Specs.Util;
using Path = Bleroy.FluentPath.Path;
namespace Orchard.Specs.Hosting {
public class WebHost {
private WebHostAgent _webHostAgent;
private Path _tempSite;
private Path _orchardWebPath;
public void Initialize(string templateName, string virtualDirectory) {
var baseDir = Path.Get(AppDomain.CurrentDomain.BaseDirectory);
_tempSite = Path.Get(System.IO.Path.GetTempFileName()).Delete().CreateDirectory();
// Trying the two known relative paths to the Orchard.Web directory.
// The second one is for the target "spec" in orchard.proj.
_orchardWebPath = baseDir.Up(3).Combine("Orchard.Web");
if (!_orchardWebPath.Exists) {
_orchardWebPath = baseDir.Parent.Combine("stage");
}
baseDir.Combine("Hosting").Combine(templateName)
.DeepCopy(_tempSite);
_orchardWebPath.Combine("bin")
.ShallowCopy("*.dll", _tempSite.Combine("bin"))
.ShallowCopy("*.pdb", _tempSite.Combine("bin"));
baseDir
.ShallowCopy("*.dll", _tempSite.Combine("bin"))
.ShallowCopy("*.exe", _tempSite.Combine("bin"))
.ShallowCopy("*.pdb", _tempSite.Combine("bin"));
HostName = "localhost";
PhysicalDirectory = _tempSite;
VirtualDirectory = virtualDirectory;
_webHostAgent = (WebHostAgent)ApplicationHost.CreateApplicationHost(typeof(WebHostAgent), VirtualDirectory, PhysicalDirectory);
}
public void Dispose() {
if (_webHostAgent != null) {
_webHostAgent.Shutdown();
_webHostAgent = null;
}
}
public void CopyExtension(string extensionFolder, string extensionName) {
var sourceModule = _orchardWebPath.Combine(extensionFolder).Combine(extensionName);
var targetModule = _tempSite.Combine(extensionFolder).Combine(extensionName);
sourceModule.ShallowCopy("*.txt", targetModule);
if (sourceModule.Combine("Views").IsDirectory)
sourceModule.Combine("Views").DeepCopy(targetModule.Combine("Views"));
}
public string HostName { get; set; }
public string PhysicalDirectory { get; private set; }
public string VirtualDirectory { get; private set; }
public string Cookies { get; set; }
public void Execute(Action action) {
var shuttleSend = new SerializableDelegate<Action>(action);
var shuttleRecv = _webHostAgent.Execute(shuttleSend);
CopyFields(shuttleRecv.Delegate.Target, shuttleSend.Delegate.Target);
}
private static void CopyFields<T>(T from, T to) where T : class {
if (from == null || to == null)
return;
foreach (FieldInfo fieldInfo in from.GetType().GetFields()) {
var value = fieldInfo.GetValue(from);
fieldInfo.SetValue(to, value);
}
}
}
}