问题描述
我有一个 Visual Studio 项目,该项目引用了我也创建的程序集.下面是项目中我的程序集引用的属性的屏幕截图.当我将程序集版本更新为 1.1.0.0
时,我的项目失败了,我认为这个属性是问题所在.
由于 Version
属性表示 1.0.0.0
,这是否意味着它会始终查找具有 1.0.0.0
?将我的版本增加到 1.1.0.0
会导致我的项目根本看不到程序集吗?
您有 2 个版本的强命名程序集.其他项目的设计行为是针对此类程序集的一个版本进行编译,无法加载具有不同版本的程序集.原因是版本更改表示 API 更改.否则,它将使用相同版本进行就地更新 - 因此较旧的代码可能无法与较新的 DLL 一起正常运行.
选项:
- 如果没有 API 更改 - 不要更改程序集版本.这意味着您可以弃用方法,但不能添加/删除方法/类或改变行为.
- 如果您根据该程序集控制所有项目 - 使用新参考重建所有项目并停止支持旧版本(如果可能).
- 在设置中提供发布商政策,以将旧版本的请求重定向到新的一个.这假设您的新版本确实向后兼容旧版本.
- 在 GAC 中安装所有版本,或者通过在每个可执行文件旁边放置正确的版本来确保应用程序获得正确的程序集版本.当您不控制程序集的所有用户并且 API 发生重大变化时,这几乎是唯一的方法.
旁注:根据所讨论的程序集是用于内部(您使用该程序集控制所有项目)还是外部使用,您可能需要做更多工作来确保向后兼容性和适当的弃用政策.
发布商政策示例:来自上面链接的文章:
<运行时><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><依赖程序集><assemblyIdentity name="myAssembly"publicKeyToken="32ab4ba45e0a69a1"文化=en-us"/><!-- 重定向到程序集的 2.0.0.0 版.--><bindingRedirect oldVersion="1.0.0.0"newVersion="2.0.0.0"/></dependentAssembly></assemblyBinding></运行时></配置>I have a Visual Studio project that references an assembly that I also created. Below is a screen shot of the properties of my assembly reference in the project. When I update my assembly version to 1.1.0.0
, my project fails, and I am thinking this property is the issue.
Since the Version
attribute says 1.0.0.0
, does this mean it will always look for my assembly that has a version of 1.0.0.0
? And incrementing my version to 1.1.0.0
will cause my project to not see the assembly at all?
You have 2 versions of strongly named assembly. It is by design behavior for other projects that where compiled against one version of such assembly to fail to load assembly with different version. The reasoning is that version change denotes API change. Otherwise it would be in-place update with the same version - so older code may not be able to function correctly with newer DLL.
Options:
- If there is no API changes - don't change version of assembly. It means you may deprecate methods, but no add/remove of methods/classes or change in behavior.
- If you control all projects depending on that assembly - rebuild all with new reference and stop supporting older version (if possible).
- Provide publisher policy in settings to redirect request for older version to new one. This assumes your new version is really backward compatible with old one.
- Install all versions in the GAC or just make sure applications get correct version of assembly by putting correct one next to each executable. It is pretty much only approach when you don't control all users of the assembly and there are significant changes in API.
Side note: depending on if the assembly in question is for internal (you control all projects using the assembly) or external consumption you may need to do more work on ensuring backward compatibility and proper deprecation policy.
Publisher policy sample: from article linked above:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="en-us" />
<!-- Redirecting to version 2.0.0.0 of the assembly. -->
<bindingRedirect oldVersion="1.0.0.0"
newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
这篇关于项目参考属性 - 版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!