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

使用ApplicationHost.CreateApplicationHost()创建Asp.Net帖子,

发布时间:2020-12-16 09:58:16 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试使用 CreateApplicationHost为一个简单的Web服务器创建一个Asp.Net应用程序主机,但是当它尝试创建我的编组对象的实例时,我得到一个System.IO.FileNotFoundException: host = (MyMarshaller)ApplicationHost.CreateApplicationHost((MyMarshaller)
我正在尝试使用 CreateApplicationHost为一个简单的Web服务器创建一个Asp.Net应用程序主机,但是当它尝试创建我的编组对象的实例时,我得到一个System.IO.FileNotFoundException:

host = (MyMarshaller)ApplicationHost.CreateApplicationHost((MyMarshaller),virtualDir,physicalDir);

我相信这个错误是因为它无法找到包含类MyMarshaller的程序集 – 如果我把这个程序集放在physicalDir的bin目录中,那么一切正常,但是我不想这样做(我也不想将我的程序集放在GAC中).

有什么方法可以控制CreateApplicationHost查找程序集的位置吗? (注意,MyMarshaller类与上面的代码行在同一个程序集中进行了对比)

解决方法

简短的回答是不可能做到这一点,但 Cassini消息来源揭示了另一种解决方案.

更新

以上链接不再有效,但CodePlex上的CassiniDev project似乎非常接近(可能是衍生作品)并包含相同的解决方案.

解决方案是避免使用CreateApplicationHost并“自己动手” – 在这种情况下使用反射来创建内部“BuildManagerHost”类型的实例并调用“RegisterAssembly”,就像这样

/// <remarks>
/// This is Dmitry's hack to enable running outside of GAC.
/// There are some errors being thrown when running in proc
/// </remarks>
private object CreateWorkerAppDomainWithHost(string virtualPath,string physicalPath,Type hostType,int port)
{
    // create BuildManagerHost in the worker app domain
    //ApplicationManager appManager = ApplicationManager.GetApplicationManager();
    Type buildManagerHostType = typeof(HttpRuntime).Assembly.GetType("System.Web.Compilation.BuildManagerHost");
    IRegisteredObject buildManagerHost = ApplicationManager.CreateObject(_appId,buildManagerHostType,virtualPath,physicalPath,false);

    // call BuildManagerHost.RegisterAssembly to make Host type loadable in the worker app domain
    buildManagerHostType.InvokeMember("RegisterAssembly",BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic,null,buildManagerHost,new object[] { hostType.Assembly.FullName,hostType.Assembly.Location });

    // create Host in the worker app domain
    // FIXME: getting FileLoadException Could not load file or assembly 'WebDev.WebServer20,Version=4.0.1.6,Culture=neutral,PublicKeyToken=f7f6e0b4240c7c27' or one of its dependencies. Failed to grant permission to execute. (Exception from HRESULT: 0x80131418)
    // when running dnoa 3.4 samples - webdev is registering trust somewhere that we are not
    return ApplicationManager.CreateObject(_appId,hostType,false);
}

(编辑:李大同)

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

    推荐文章
      热点阅读