代码之家  ›  专栏  ›  技术社区  ›  Justin

使用ApplicationHost.CreateApplicationHost()创建Asp.Net post,同时从其他位置加载程序集

  •  4
  • Justin  · 技术社区  · 15 年前

    我试着用 CreateApplicationHost System.IO.FileNotFoundException 当它试图创建编组对象的实例时:

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

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

    MyMarshaller 类与上述代码行位于同一程序集中)

    2 回复  |  直到 15 年前
        1
  •  5
  •   Justin    12 年前

    简而言之,这是不可能做到的,但是 Cassini 消息人士透露了另一种解决方案。


    CassiniDev project 在CodePlex上似乎非常接近(可能是一个衍生作品),并且包含相同的解决方案。

    解决方法是避免使用 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);
    }