【问题标题】:.NET use application config file to load an assembly that is referenced by another assembly.NET 使用应用程序配置文件加载另一个程序集引用的程序集
【发布时间】:2018-08-23 15:25:00
【问题描述】:

我的原生程序是 temp/A/prog.exe。我在 temp/B/LoadAssem.dll 中使用 C++/CLI 中的 Assembly::Load() 加载 .Net 程序集。但是 assem.dll 引用了位于 temp/C 中的 dynAssem.dll。我编写的配置文件如下:

<configuration>  
   <runtime>  
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
       <dependentAssembly>  
         <assemblyIdentity name="DynAssem"  
                           publicKeyToken="xxxxx"  
                           culture="neutral" />  
         <codeBase version="0.0.0.0" href="../C/DynAssem.dll"/>  
       </dependentAssembly>        
      </assemblyBinding>  
   </runtime>  
</configuration>

但绑定失败,融合输出如下:

=== Pre-bind state information ===
LOG: DisplayName = dynAssem
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: dynAssem | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///c:/temp/A/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = prog.exe
Calling assembly : XXX, Version=0.0.0.0, Culture=neutral, PublicKeyToken=XXX.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: c:\temp\A\prog.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///c:/temp/A/dynAssem.DLL.
LOG: Attempting download of new URL file:///c:/temp/A/dynAssem/dynAssem.DLL.
LOG: Attempting download of new URL file:///c:/temp/A/dynAssem.EXE.
LOG: Attempting download of new URL file:///c:/temp/A/dynAssem/dynAssem.EXE.
LOG: All probing URLs attempted and failed.

我了解此目录布局不是 .NET 的首选布局。但是,我需要支持多种其他语言,因此无法更改目录结构和程序集位置。

【问题讨论】:

    标签: c# .net config .net-assembly assembly-loading


    【解决方案1】:

    您可能希望通过 app.config 中的探测元素实现这一点(请参阅 https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/probing-element

    <configuration>  
       <runtime>  
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
            <probing privatePath="../C/"/>  
           <dependentAssembly>  
             <assemblyIdentity name="DynAssem"  
                               publicKeyToken="xxxxx"  
                               culture="neutral" />  
             <codeBase version="0.0.0.0" href="../C/DynAssem.dll"/>  
           </dependentAssembly>        
          </assemblyBinding>  
       </runtime>  
    </configuration>
    

    但似乎(至少根据Probing the assembly from parent directory of the exe)无法探测文件夹层次结构中更高的文件夹。

    编辑:

    如此处所述 (https://blogs.msdn.microsoft.com/suzcook/2003/05/29/choosing-a-binding-context/),您可以使用 LoadFrom 从您喜欢的任何路径加载程序集,我没有尝试过,但这可能是您的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 2012-08-13
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 2010-12-09
      相关资源
      最近更新 更多