加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 并排装配加载问题

发布时间:2020-12-15 17:14:45 所属栏目:百科 来源:网络整理
导读:我有一个简单的应用程序,通过这段代码从运行时从2个子文件夹加载两个程序集: Assembly.Load("A,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null");Assembly.Load("B,PublicKeyToken=null"); 目录结构是: 所以预期的载荷如下: TheApp.exe - A.dll -
我有一个简单的应用程序,通过这段代码从运行时从2个子文件夹加载两个程序集:
Assembly.Load("A,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null");
Assembly.Load("B,PublicKeyToken=null");

目录结构是:

所以预期的载荷如下:

TheApp.exe -> A.dll -> C.dll (version 2.0.0.0)
           -> B.dll -> C.dll (version 1.0.0.0)

请注意C.dll已签名,因此两个版本应并排加载.

为确保应用程序从正确的位置加载程序集,我将以下内容添加到应用程序配置文件中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="B;A" />
    </assemblyBinding>
  </runtime>
</configuration>

问题是每当启动时应用程序都会崩溃并显示以下消息:

=== Pre-bind state information ===
LOG: User = ...
LOG: DisplayName = C,Version=2.0.0.0,PublicKeyToken=93a02044a09d059a
 (Fully-specified)
LOG: Appbase = file:///D:/Temp/TheApp/bin/Debug/Test/
LOG: Initial PrivatePath = NULL
Calling assembly : A,PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:TempTheAppbinDebugTestTheApp.exe.Config
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv2.0.50727configmachine.config.
LOG: Post-policy reference: C,PublicKeyToken=93a02044a09d059a
LOG: Attempting download of new URL file:///D:/Temp/TheApp/bin/Debug/Test/C.DLL.
LOG: Attempting download of new URL file:///D:/Temp/TheApp/bin/Debug/Test/C/C.DLL.
LOG: Attempting download of new URL file:///D:/Temp/TheApp/bin/Debug/Test/B/C.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

问题:为什么运行时仅查看“B”文件夹?为什么不继续在A文件夹中查找正确版本的共享程序集?

EDIT1:我添加了< codeBase>标签如下所述,我知道在我的配置文件中有以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <probing privatePath="B;A" />
    </assemblyBinding>
    <dependentAssembly>
       <assemblyIdentity name="C" publicKeyToken="93a02044a09d059a" /> 
       <codeBase version="1.0.0.0" href="B/C.dll"/>
       <codeBase version="2.0.0.0" href="A/C.dll"/>
    </dependentAssembly>
  </runtime>
</configuration>

问题仍然存在!

解决方法

请参阅有关探测的 this MSDN页面上的注释,该注释直接解决了您的问题:

If you have multiple versions of an assembly in a directory and you want to reference a particular version of that assembly,you must use the <codeBase> element instead of the privatePath attribute of the <probing> element. If you use the <probing> element,the runtime stops probing the first time it finds an assembly that matches the simple assembly name referenced,whether it is a correct match or not. If it is a correct match,that assembly is used. If it is not a correct match,probing stops and binding fails.

运行时正在寻找2.0.0.0版本但找到1.0.0.0版本并停止查找.

最终的解决方案是将配置文件更改为以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <probing privatePath="B;A" />
     <dependentAssembly>
       <assemblyIdentity name="C" publicKeyToken="93a02044a09d059a" /> 
       <codeBase version="1.0.0.0" href="B/C.dll"/>
       <codeBase version="2.0.0.0" href="A/C.dll"/>
     </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读