.NET使用应用程序配置文件来加载另一个程序集引用的程序集

时间:2018-03-14 22:55:29

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

我的原生程序是temp / A / prog.exe。我正在使用C ++ / CLI中的Assembly :: Load()在temp / B / LoadAssem.dll上加载.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的首选布局。但是,我需要支持多种其他语言,因此无法更改目录结构和程序集位置。

1 个答案:

答案 0 :(得分:0)

您可能希望通过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从您喜欢的任何路径加载程序集,我没有尝试过,但这可能是您的解决方案。

相关问题