簡體   English   中英

從另一個目錄將程序集加載到臨時Appdomain

[英]Load assembly to temporary Appdomain from another directory

作為BaaS項目的一部分,我需要從“guest”程序集動態執行代碼。 我已經嘗試了其他類似問題的所有示例和答案,包括AppDomainToolKit方法,但沒有運氣。

與插件方法類似,我通過將相關程序集復制到主機應用程序的bin路徑中獲得了積極的結果。 但是,對於當前場景,這是不可能的:

  • 每個請求都需要應用權限和限制
  • 應在臨時appdomain中評估每個請求
  • 從路徑到臨時域加載所需的組件和引用的類型

到目前為止,我的最新一段代碼如下

        // ClassLibrary1.dll and ClassLibrary2.dll are in the same directory, both are marked as Serializable

        var binPath =  @"C:\AssemblyDemo\ClassLibrary1\bin\Debug\";
        var lib1Path = binPath + "ClassLibrary1.dll";
        var lib2Path = binPath + "ClassLibrary2.dll";

        var setup = new AppDomainSetup();
        setup.ApplicationBase = binPath;
        AppDomain domain = AppDomain.CreateDomain("domainname", null, setup);
        ObjectHandle handle = domain.CreateInstanceFrom(lib1Path, "ClassLibrary1.Class1");
        var unwrap = handle.Unwrap();
        var m1 = unwrap.GetType().GetMethod("Method1");
        var result = m1.Invoke(unwrap, null);

handle.Unwrap()拋出異常Type is not resolved for member 'ClassLibrary1.Class1,ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

如果只想在臨時AppDomain中加載程序集,則無法在主域中調用Unwrap Unwrap也在那里加載類型。 您必須將對象的所有訪問權限從臨時程序集移動到臨時AppDomain。 您可以使用AppDomain.DoCallBack或您在子域中實例化並從父域調用的自定義MarshalByRef類來執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM