将 .NET 程序集引用解析为不同的名称?

发布于 2024-10-04 06:19:02 字数 673 浏览 3 评论 0 原文

我的项目引用了Library1.dllLibrary2.dllLibrary2.dll 依赖于 Library1.dll,但它被编译为通过不同的名称 Library1.Net40.dll 来引用它。

有没有一种好的方法告诉我的应用程序将 Library1.Net40.dll 的所有引用重定向到 Library1.dll?也许类似于使用 重定向版本的方式?

我有一个处理 AppDomain.AssemblyResolve 事件的解决方案,但这有点麻烦,我希望有更好的方法来做到这一点。

编辑: 供任何人参考,以下是我最终使用 AppDomain.AssemblyResolve 事件重定向到不同的程序集。

My project references Library1.dll and Library2.dll. Library2.dll has a dependency on Library1.dll, but it was compiled to reference it by a different name, Library1.Net40.dll.

Is there an nice way tell my application to redirect all references for Library1.Net40.dll to resolve to Library1.dll? Maybe something similar to the way you can redirect versions using a <bindingRedirect>?

I've got a solution that handles the AppDomain.AssemblyResolve event, but it's a bit of a hack and am hoping there's a better way to do this.

Edit:
For anyone's reference, here's how I ended up solving it using the AppDomain.AssemblyResolve event to redirect to a different assembly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2024-10-11 06:19:02

您是否尝试过使用 元素

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

(未经测试;不知道它是否有效。)

CF:我将此更新放在这里,因为评论有点长:)

好主意,谢谢。我的重定向工作正常,但它抱怨因为名称不同,日志如下:

LOG: Attempting download of new URL file:///C:/Project/bin/Library1.dll.
LOG: Assembly download was successful. Attempting setup of file: C:\Project\bin\Library1.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Library1, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
WRN: Comparing the assembly name resulted in the mismatch: NAME
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Have you tried playing with the <codeBase> element?

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

(Untested; no idea if it works.)

CF: Am putting this update here, because it's a bit long for the comments :)

Good idea, thanks. I got the redirect working but it complains because the names are different, here's the log:

LOG: Attempting download of new URL file:///C:/Project/bin/Library1.dll.
LOG: Assembly download was successful. Attempting setup of file: C:\Project\bin\Library1.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Library1, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
WRN: Comparing the assembly name resulted in the mismatch: NAME
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
夢归不見 2024-10-11 06:19:02

应用PARTIAL解析时,程序集名称必须与文件名匹配。但是,文件的位置可能不同。

否则 Fusion 绑定日志将报告“WRN:比较程序集名称导致不匹配:NAME”并且无法绑定。

(好消息:可以重命名程序集 DLL 以匹配程序集名称。)

例如:

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="6.0.0.0" />
    <bindingRedirect oldVersion="11.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
    <codeBase version="12.0.0.0" href="bin/Newtonsoft.Json.12/Newtonsoft.Json.dll" />
  </dependentAssembly>

这将解析 bin/Newtonsoft.Json.dllbin /Newtonsoft.Json.12/Newtonsoft.Json.dll,具体取决于版本(分别为 6-10 或 11-12)。即使目录路径不同,NAME 也成功匹配文件名

注意“bin”本身就是替代版本的 href 的一部分;根据应用基础进行相关调整,这与探测路径不同。对于在 IIS 下运行的 about 来说,应用程序库位于 bin 目录之上。 (请参阅 Fusion 日志中的“LOG: Appbase = ..”。)

不幸的是,无论配置文件如何,MSBuild 进程都不会自动遵循引用程序集的目录结构。将项目设置为“复制本地”替代程序集版本,然后将它们复制为辅助流程的一部分,确保维护正确的结构。如果任何已编译的程序集将备用版本作为直接引用,那么最好确保默认情况下没有一个版本是“复制本地”。

When a PARTIAL resolve is applied, the ASSEMBLY NAME must match the filename. However the LOCATION of the file can be different.

Otherwise the Fusion Binding Log will report "WRN: Comparing the assembly name resulted in the mismatch: NAME" and be unable to bind.

(Good news: it is possible to rename the assembly DLL to match the Assembly Name.)

For example:

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="6.0.0.0" />
    <bindingRedirect oldVersion="11.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
    <codeBase version="12.0.0.0" href="bin/Newtonsoft.Json.12/Newtonsoft.Json.dll" />
  </dependentAssembly>

This resolves bin/Newtonsoft.Json.dll and bin/Newtonsoft.Json.12/Newtonsoft.Json.dll, depending on version (6-10 or 11-12, respectively). The NAME successfully matches the filename even though the directory path is different.

N.B. "bin" is itself part of the href to the alternate version; adjust as relevant depending on the application base, which is different than the probe path. In the case about, which is running under IIS, the app base is a level above the bin directoy. (See "LOG: Appbase = .." in the Fusion Log.)

Unfortunately the MSBuild process does not honor the directory structure of Referenced Assemblies automatically, regardless of any configuration files. Set the project to not "Copy Local" the alternative assembly versions and then copy them as part of a secondary process ensuring the correct structure is maintained. If any compiled assembly take the alternate version as a directly reference it might be good to ensure that none are "Copy Local" by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文