如何加载 +卸载 ASPNET 运行时,不使用 bin 目录,用于自动化测试目的
我想加载 ASPNET 运行时,运行一个或多个页面,然后卸载它。这是出于测试目的。这不是 UI 测试;而是 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的答案,但似乎做了很多工作来避免一点。还有其他想法吗?
I want to load the ASPNET runtime, run one or more pages, then unload it. This is for testing purposes. It's not UI testing; I'm really just testing the use of a library in an ASPNET context.
Normally this kind of thing is done with a call to System.Web.Hosting.ApplicationHost.CreateApplicationHost
.
This is how I have it currently:
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);
}
}
}
}
}
This is the custom ASP.NET host:
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();
}
}
This works ok. But I'd like to avoid the creation of the bin directory, and the copying of the assembly into it, if possibble.
Is it possible to use CreateApplicationHost
, and tell ASPNET to load assemblies from the current directory, or an arbitrary directory, rather than bin
?
EDIT :: simplified the code a bit.
EDIT2 :: I looked at womp's answer but it seems like doing a lot of work to avoid a little. Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个很好的问题。我记得几年前必须处理类似的问题(我想让应用程序主机使用自定义配置文件)并添加书签 这篇博文是我用来开始的。
对于您真正想要的东西来说,这可能有点矫枉过正,但也许它可以为您指明正确的方向。
This is a great question. I remember having to deal with a similar issue a couple years ago (I wanted to have an applicationhost use a custom config file) and bookmarked this blog post which I used to get me started.
It's probably way overkill for what you actually want, but perhaps it can point you in the right direction.
我发现的另一个选择是创建一个虚拟 web.config,将 bin 文件夹添加为探测文件夹。我在.NET2下尝试过这个。请参阅:https://stackoverflow.com/a/12169675/793493
Another option that I found out was to create a dummy web.config that added the bin folder as the probing folder. I tried this under .NET2. See this: https://stackoverflow.com/a/12169675/793493