使用 ApplicationHost.CreateApplicationHost() 创建 Asp.Net 帖子,同时从另一个位置加载程序集

发布于 2024-09-09 05:24:04 字数 629 浏览 5 评论 0原文

我正在尝试使用 CreateApplicationHost 为简单的 Web 服务器创建 Asp.Net 应用程序主机,但是当它尝试创建编组对象的实例时,我收到 System.IO.FileNotFoundException

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

我相信这个错误是因为它找不到包含 MyMarshaller 类的程序集 - 如果我将此程序集放在physicalDir 的 bin 目录中,那么一切都会正常工作,但是我不想这样做(我也不想将我的程序集放入 GAC 中)。

有什么方法可以控制 CreateApplicationHost 在哪里查找我的程序集? (请注意,MyMarshaller 类与上述代码行位于同一程序集中)

I'm trying to use CreateApplicationHost to create an Asp.Net application host for a simple web server, however I'm getting a System.IO.FileNotFoundException when it attempts to create an instance of my marshalling object:

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

I believe that this error is because it cannot find the assembly containing the class MyMarshaller - if I put this assembly in the bin directory of physicalDir then everything works fine, however I don't want to do this (I also don't want to place my assembly in the GAC either).

Is there any way that I can control where CreateApplicationHost looks for my assembly? (Note that MyMarshaller class is conted in the same assembly as the above line of code)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2024-09-16 05:24:04

简短的答案是不可能做到这一点,但是 Cassini 源揭示了一种替代解决方案。


更新

上面的链接不再有效,但是 CassiniDev 项目 似乎非常接近(可能是衍生作品)并且包含相同的解决方案。

解决方案是避免使用 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, virtualPath, physicalPath, false);
}

The short answer is that it isn't possible to do this, however the Cassini source revealed an alternative solution.


Update

The above link no longer works, however the CassiniDev project on CodePlex appears to be pretty close (possibly a derivative work) and contains the same solution.

The solution is to avoid using CreateApplicationHost and "do it yourself" instead - in this case use reflection to create an instance of the internal "BuildManagerHost" type and call "RegisterAssembly", like so

/// <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, virtualPath, physicalPath, false);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文