【问题标题】:How to load + unload ASPNET runtime, without using bin directory, for automated testing purposes如何加载 + 卸载 ASPNET 运行时,而不使用 bin 目录,用于自动化测试目的
【发布时间】:2009-09-07 19:15:02
【问题描述】:

我想加载 ASPNET 运行时,运行一个或多个页面,然后卸载它。这是出于测试目的。这不是 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 加载程序集?

EDIT :: 稍微简化了代码。 EDIT2 :: 我查看了 womp 的答案,但似乎做了很多工作来避免一点点。还有其他想法吗?

【问题讨论】:

    标签: asp.net testing appdomain


    【解决方案1】:

    这是一个很好的问题。我记得几年前不得不处理一个类似的问题(我想让一个应用程序主机使用自定义配置文件)并标记了this blog post,我曾经用它来帮助我入门。

    对于你真正想要的东西来说,这可能有点矫枉过正,但也许它可以为你指明正确的方向。

    【讨论】:

    • 哇,新进程、环境变量、GAC... 听起来像是要避免我创建和删除目录的大量工作。这是正确的想法……我得看看我是否可以简化它。
    【解决方案2】:

    我发现的另一个选项是创建一个虚拟 web.config,将 bin 文件夹添加为探测文件夹。我在.NET2下试过这个。看到这个:https://stackoverflow.com/a/12169675/793493

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多