ASPNET ランタイムをロードし、1 つまたは複数のページを実行してから、アンロードしたいと考えています。これはテスト用です。UI テストではありません。ASPNET コンテキストでのライブラリの使用を実際にテストしているだけです。
通常、この種のことは への呼び出しで行われSystem.Web.Hosting.ApplicationHost.CreateApplicationHost
ます。
これは私が現在持っている方法です:
public class Manager : System.MarshalByRefObject
{
private void HostedDomainHasBeenUnloaded(object source, System.EventArgs e)
{
aspNetHostIsUnloaded.Set();
}
private ManualResetEvent aspNetHostIsUnloaded;
public void Run(string[] pages)
{
bool cleanBin = false;
MyAspNetHost host = null;
try
{
if (!Directory.Exists("bin"))
{
cleanBin = true;
Directory.CreateDirectory("bin");
}
var a = System.Reflection.Assembly.GetExecutingAssembly();
string destfile= Path.Combine("bin", Path.GetFileName(a.Location));
File.Copy(a.Location, destfile, true);
host =
(MyAspNetHost) System.Web.Hosting.ApplicationHost.CreateApplicationHost
( typeof(MyAspNetHost),
"/foo", // virtual dir
System.IO.Directory.GetCurrentDirectory() // physical dir
);
aspNetHostIsUnloaded = new ManualResetEvent(false);
host.GetAppDomain().DomainUnload += this.HostedDomainHasBeenUnloaded;
foreach (string page in pages)
host.ProcessRequest(page);
}
finally
{
// tell the host to unload
if (host!= null)
{
AppDomain.Unload(host.GetAppDomain());
// wait for it to unload
aspNetHostIsUnloaded.WaitOne();
// remove the bin directory
if (cleanBin)
{
Directory.Delete("bin", true);
}
}
}
}
}
これはカスタム ASP.NET ホストです。
public class MyAspNetHost : System.MarshalByRefObject
{
public void ProcessRequest(string page)
{
var request = new System.Web.Hosting.SimpleWorkerRequest(page, // page being requested
null, // query
System.Console.Out // output
);
System.Web.HttpRuntime.ProcessRequest(request);
}
public AppDomain GetAppDomain()
{
return System.Threading.Thread.GetDomain();
}
}
これは正常に動作します。しかし、可能であれば、bin ディレクトリの作成とアセンブリのコピーを避けたいと思います。
CreateApplicationHost
を使用して、現在のディレクトリまたは任意のディレクトリからアセンブリをロードするように ASPNET に指示することは可能bin
ですか?
編集::コードを少し簡略化しました。 EDIT2 :: womp 's answer を見ましたが、少し回避するために多くの作業を行っているようです。他のアイデアはありますか?