简体   繁体   English

控制 DLL 版本兼容性以获得更大的灵活性

[英]Controlling DLL version compatibility for more flexibility

I have two C# DLLs - an "api" dll, a collection of interfaces for an application, and a "plugin" dll, code that uses the API to extend our application.我有两个 C# DLL——一个“api”dll,一个应用程序接口的集合,一个“插件”dll,使用 API 来扩展我们的应用程序的代码。

Whenever I make a new "api" build, the plugin dll fails to load in our application:每当我进行新的“api”构建时,插件 dll 都无法在我们的应用程序中加载:

The classes in the module cannot be loaded.无法加载模块中的类。

To fix this, we simply have to rebuild the plugin (since it's using the "latest" API dll.为了解决这个问题,我们只需要重新构建插件(因为它使用“最新的”API dll。

My guess is it has to do with how Visual Studio or the compiler is versioning the DLLs.我的猜测是它与 Visual Studio 或编译器如何对 DLL 进行版本控制有关。 Right now the API dll is versioned as 1.0.6469.2848 which is automated.现在 API dll 的版本为1.0.6469.2848 ,这是自动化的。

I need a bit more flexibility but I don't know how to go about it.我需要更多的灵活性,但我不知道如何去做。 I'm choosing the wrong terminology when looking for solutions.我在寻找解决方案时选择了错误的术语。

Preferably, I'd like something closer to semver - my plugin DLL should continue to load/work if it was compiled against api version 1.0.1, even if my application has api version 1.0.2, or 1.1.最好,我想要更接近semver 的东西 - 如果我的插件 DLL 是针对 api 版本 1.0.1 编译的,它应该继续加载/工作,即使我的应用程序具有 api 版本 1.0.2 或 1.1。 Only 2.x, "backwards-compatibility breaking API changes" should be rejected.只有 2.x,“向后兼容破坏 API 更改”应该被拒绝。

As I think yo have found out, it is the assembly version that is messing you up.正如我认为您已经发现的那样,是汇编版本让您感到困惑。 This is because the plugin's version build/macro number has incremented and the application was built against a previous version.这是因为插件的版本构建/宏编号增加了,并且应用程序是针对以前的版本构建的。

You could deal with this in a couple of ways.您可以通过多种方式处理此问题。

  • In the application config you can do a binding redirect so that it loads up the newer version.在应用程序配置中,您可以进行绑定重定向,以便加载较新的版本。
<dependentAssembly>
  <assemblyIdentity name="someAssembly"
     publicKeyToken="32ab4ba45e0a69a1" culture="en-us" />  
  <bindingRedirect oldVersion="1.0.0.0-7.0.0.0" newVersion="8.0.0.0" />  
</dependentAssembly>  
  • Alternatively and probably the route I would take and sticking to semver I would manually control the AssemblyVersionAttribute (1.2.3) only incrementing on breaking changes.或者,可能是我将采用的路线并坚持使用 semver 我将手动控制AssemblyVersionAttribute (1.2.3) 仅在中断更改时递增。 The AssemblyFileVersionAttribute I would increment with the build so that you can determine the actual build version of a dll.我会随着构建而增加AssemblyFileVersionAttribute ,以便您可以确定 dll 的实际构建版本。 You can change this in the AssemblyInfo.cs您可以在AssemblyInfo.cs更改此设置

I did answer another question on assembly versioning that might help.我确实回答了另一个关于程序集版本控制的问题,这可能会有所帮助。

Note注意

Depending on how you are building your assemblies (locally or on a build server) how you generate the version number will differ.根据您构建程序集的方式(在本地或在构建服务器上),您生成版本号的方式会有所不同。 If you are building locally you will need to, as Sylence pointed out in his answer, change the wild card in the AssemblyVersionAtribute .如果您在本地构建,则需要像 Sylence 在他的回答中指出的那样,更改AssemblyVersionAtribute的通配符。 If you are using a build server such as TeamCity, Jenkins, et al.如果您使用的是构建服务器,例如 TeamCity、Jenkins 等。 then you can completely overwrite the assembly and file versions prior to the compile.那么您可以在编译之前完全覆盖程序集和文件版本。

Look for an AssemblyInfo.cs file in your API project.在您的 API 项目中查找AssemblyInfo.cs文件。 There you have an AssemblyVersion and AssemblyFileVersion that you can set to whatever you want.在那里你有一个AssemblyVersionAssemblyFileVersion ,你可以设置为任何你想要的。 Just make sure you don't use a * in the version.只要确保您不在版本中使用 * 即可。 This way the version will keep the same after each build.这样,版本将在每次构建后保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM