如何关闭 .net 应用程序的程序集版本检查

发布于 2025-01-17 06:36:18 字数 1904 浏览 3 评论 0原文

我是 C#/.net 程序的开发人员,该程序可以作为独立应用程序运行,也可以作为第三方应用程序的插件运行。

由于在将程序作为插件运行时进行版本检查,我在程序集加载方面遇到了问题。

我使用的一些 NuGet 包依赖于 Newtonsoft Json,但版本不同。假设 NuGet 包 A 需要 Newtonsoft Json >= 12,NuGet 包 B 需要 Newtonsoft Json >= 13。
当我独立运行程序时,这没有问题,因为 NuGet 包声明依赖关系为 >= 并且 Visual Studio 似乎会自动生成一些重定向信息并将其放入 dll.config 或 exe.config 文件中。
此信息如下所示:

      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
      </dependentAssembly>

但显然,当我的程序作为第 3 方应用程序的插件加载时,此重定向信息将被忽略,并且 >= 依赖项通过某种黑魔法变成 == 依赖项。
因此,当我在版本 12 中使用 Newtonsoft json lib 时,由于版本错误,使用 NuGet 包 B 中的内容的代码因程序集加载异常而崩溃
如果我使用版本 13 中的 Newtonsoft json lib(这当然是我想要使用的,因为它是较新的版本),则使用 NuGet 包 A 中的内容的代码会因版本错误而崩溃并出现程序集加载异常。

因此,仅仅通过将我的程序作为插件加载,我就陷入了某种 dll 地狱。

我现在知道解决此问题的 2 个不好的方法:

  1. 我可以将 NuGet 包 B 降级到也依赖于 Newtonsoft Json >= 12 的旧版本
  2. 我可以编辑 3rdParty.exe.config 文件来添加
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
      </dependentAssembly>

现在一切运行良好

1 )是不好的,因为 NuGet 包的任何更新都可能导致程序集加载异常,该异常仅在实际使用功能时在运行时检测到
2) 不好,因为我必须编辑一个我无法控制的文件,所以每次更新 3rdParty 程序时我都必须再次编辑它

这个问题有更好的解决方案吗?

我可以在 Visual Studio 或 Windows 或 .NET 或任何地方在程序集加载期间关闭版本检查,以便 >= 依赖项不会变成 == 依赖项吗?

我的程序有很多依赖项,Newtonsoft json 是仅举一例。多年来,该项目一直受到这个 dll 地狱的困扰,没有开发人员敢于接触具有明显缺点的 NuGet 包版本。
但是,当开发人员添加/更新软件包时,我们确实会遇到所描述的问题。

我的程序是用 C# 编写的,目标是 64 位和 64 位。 .net 框架 4.7.2。并在 Windows 10 上运行。我认为第 3 方程序正在使用 .net Framework 4.5.2。

预先感谢您的任何帮助

I'm a developer of a C#/.net program that can run as a standalone application but also as a plug in a 3rd party application.

I have problems with assembly loading because of versions checks when running the program as a plug in.

Some of the NuGet package I use depend on for example Newtonsoft Json but in different versions. Let’s say NuGet package A needs Newtonsoft Json >= 12 and NuGet package B needs Newtonsoft Json >= 13.
This is no problem when I run the program stand alone as the NuGet packages state the dependency is >= and Visual Studio seems to autogenerate some redirect information and puts it in the dll.config or exe.config files.
This information looks like this:

      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
      </dependentAssembly>

But apparently when my program is loaded as a plug in by the 3rd Party Application this redirect info gets ignored and the >= dependencies turn, by some black magic, into == dependencies.
So when I use the Newtonsoft json lib in version 12 my code using stuff from NuGet package B crashes with a assembly load exceptions because of the wrong version
and if I use the Newtonsoft json lib in version 13 (which is of course what I want to use as it is the newer version) my code using stuff from NuGet package A crashes with a assembly load exceptions because of the wrong version.

So just by having my program be loaded as a plugin I got into some kind of dll hell.

I now know of 2 bad ways to fix this:

  1. I can down grade NuGet package B to an older version that also depends on Newtonsoft Json >= 12
  2. I can edit the 3rdParty.exe.config file to add
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
      </dependentAssembly>

And everything runs nicely

Now 1) is bad because any update of a NuGet package might lead to a assembly load exception which is only detected at run time when the actually feature is used
and 2) is bad because I have to edit a file I don't control, so every time I update the 3rdParty program I have to edit it again

Are there any better solutions to this problem?

Can I somehow in Visual Studio or Windows or .NET or wherever switch off the version check during assembly loading so that >= dependencies don't turn into == dependencies?

My program has many dependencies and Newtonsoft json was only one example. The project is haunted by this dll hell since years now and no developer dares to touch the NuGet packages versions which has obvious downsides.
But when a developer does add/update packages we do run into the described problems

My program is written in C# and targeting 64 bit & .net Framework 4.7.2. and running on Windows 10. I think the 3rd Party Program is using .net Framework 4.5.2.

Thanks in Advance for any help

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

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

发布评论

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

评论(1

讽刺将军 2025-01-24 06:36:18

恕我直言,选项 2 并不是一个坏方法 - 它是 .NET 应该工作的方式:添加依赖项时,您还必须处理传递依赖项重定向。这正是您在 app.config/web.config 中看到所有绑定重定向的原因,即使引用源自 .NET 本身的程序集(例如 System. Net.Http)。如果您不喜欢这个,请不要使用 .NET

Imho option 2 is not a bad way - it is the way how .NET is supposed to work: when adding a dependency, you have to handle transitive dependency redirects as well. That's exactly the reason why you see all the binding redirects within app.config/web.config, even when referencing assemblies originating from .NET itself (e. g. System.Net.Http). If you don't like this, don't use .NET ????

I'd highly discourage disabling the version check. The chance that you will encounter some nasty, hard to debug behaviors is quite high.

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