0

我的本机程序是 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 的首选布局。但是,我需要支持多种其他语言,因此无法更改目录结构和程序集位置。

4

1 回答 1

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

于 2018-03-15T00:56:35.077 回答