加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何在不使用bin目录的情况下加载卸载ASPNET运行时,

发布时间:2020-12-16 07:43:01 所属栏目:asp.Net 来源:网络整理
导读:我想加载ASPNET运行时,运行一个或多个页面,然后卸载它.这是出于测试目的.这不是UI测试;我真的只是在ASPNET上下文中测试库的使用. 通常这种事情是通过调用System.Web.Hosting.ApplicationHost.CreateApplicationHost来完成的. 这就是我目前的方式: public cl
我想加载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中加载程序集?

编辑::简化了一下代码.
EDIT2 ::我看了womp的答案,但似乎做了很多工作以避免一点点.还有其他想法吗?

解决方法

这是一个很好的问题.我记得几年前不得不处理类似的问题(我希望有一个应用程序主机使用自定义配置文件)并将 this blog post加入书签,我曾经让我开始使用.

对于你真正想要的东西来说,这可能有些过分,但也许它可以指向你正确的方向.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读