如何摆脱 VSIX 项目中的包引用冲突

How can I get rid of package reference conflicts in a VSIX project

我有一个包含多个项目的解决方案,它生成一个 Visual Studio 包。

部分项目和 Nuget 包如下所示。顶级 Visual Studio 扩展是 MultiLanguageWPF。

MultiLanguageWPF
| 
+-- MultiLanguageLegacy
|   |       Nuget: Microsoft.CodeAnalysis.Analyzers  Version 1.0.0 
|   |
|   +-- MultiLangCodeParser
|   |       Nuget: Microsoft.CodeAnalysis.Analyzers  Version 1.0.0
|   |       Nuget: Microsoft.CodeAnalysis.Compilers  Version 1.0.0
|   |
+---+-- MultiLangCommon
            Nuget: Microsoft.CodeAnalysis            Version 1.0.0
            Nuget: Microsoft.CodeAnalysis.Analyzers  Version 1.0.0

所有这些 Nuget 包都包含在版本 1.0.0 中。所有项目都是针对 Framework 4.7.2 构建的。

这个问题是关于为项目 MultiLanaguageWPF 生成的三个警告:

Found conflicts between different versions of "Microsoft.CodeAnalysis" that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed.

Found conflicts between different versions of "Microsoft.CodeAnalysis.VisualBasic" that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed.

Found conflicts between different versions of "Microsoft.CodeAnalysis.CSharp" that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed.

包 Microsoft.CodeAnalysis.VisualBasic 和 Microsoft.CodeAnalysis.CSharp 是包 Microsoft.CodeAnalysis.Compilers.

的依赖项

这三个警告是为项目 MultiLanaguageWPF 生成的,该项目实际上并未使用这些包。显然所有引用的 DLL 都必须被拉入顶层项目并最终打包到 VSIX 安装中,所以我想这确实有意义。

按照警告消息中的建议,我已将日志详细程度设置为详细。对于包Microsoft.CodeAnalysis,这是我看到的一些。

16>  Primary reference "Microsoft.CodeAnalysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35".
16>      Resolved file path is "C:\Users\Phil\.nuget\packages\microsoft.codeanalysis.common.0.0\lib\net45\Microsoft.CodeAnalysis.dll".
16>      Reference found at search path location "{RawFileName}".
16>      Found related file "C:\Users\Phil\.nuget\packages\microsoft.codeanalysis.common.0.0\lib\net45\Microsoft.CodeAnalysis.xml".
16>      This reference is not "CopyLocal" because at least one source item had "Private" set to "false" and no source items had "Private" set to "true".
16>      The ImageRuntimeVersion for this reference is "v4.0.30319".

16>  Unified Dependency "Microsoft.CodeAnalysis, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35".
16>      Using this version instead of original version "1.0.0.0" in "C:\MultiLang_Version_7_1\VS2013\MultiLang\bin\Debug\MultiLanguageLegacy.dll" because AutoUnify is 'true'.
16>      Using this version instead of original version "1.0.0.0" in "C:\Users\Phil\.nuget\packages\microsoft.codeanalysis.csharp.0.0\lib\net45\Microsoft.CodeAnalysis.CSharp.dll" because AutoUnify is 'true'.
16>      Using this version instead of original version "1.0.0.0" in "C:\Users\Phil\.nuget\packages\microsoft.codeanalysis.csharp.workspaces.0.0\lib\net45\Microsoft.CodeAnalysis.CSharp.Workspaces.dll" because AutoUnify is 'true'.
16>      Using this version instead of original version "1.0.0.0" in "C:\Users\Phil\.nuget\packages\microsoft.codeanalysis.visualbasic.0.0\lib\net45\Microsoft.CodeAnalysis.VisualBasic.dll" because AutoUnify is 'true'.
16>      Using this version instead of original version "1.0.0.0" in "C:\Users\Phil\.nuget\packages\microsoft.codeanalysis.visualbasic.workspaces.0.0\lib\net45\Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll" because AutoUnify is 'true'.
16>      Using this version instead of original version "1.0.0.0" in "C:\Users\Phil\.nuget\packages\microsoft.codeanalysis.workspaces.common.0.0\lib\net45\Microsoft.CodeAnalysis.Workspaces.Desktop.dll" because AutoUnify is 'true'.
16>      Using this version instead of original version "1.0.0.0" in "C:\Users\Phil\.nuget\packages\microsoft.codeanalysis.workspaces.common.0.0\lib\net45\Microsoft.CodeAnalysis.Workspaces.dll" because AutoUnify is 'true'.
16>      Using this version instead of original version "1.0.0.0" in "C:\Users\Phil\.nuget\packages\microsoft.visualstudio.languageservices.0.0\lib\net45\Microsoft.VisualStudio.LanguageServices.dll" because AutoUnify is 'true'.
16>      Using this version instead of original version "1.0.0.0" in "C:\MultiLang_Version_7_1\VS2013\MultiLang\bin\Debug\MultiLangCommon.dll" because AutoUnify is 'true'.
16>      Using this version instead of original version "1.0.0.0" in "C:\MultiLang_Version_7_1\VS2013\MultiLang\bin\Debug\MultiLangCodeParser.dll" because AutoUnify is 'true'.
16>      Could not resolve this reference. Could not locate the assembly "Microsoft.CodeAnalysis, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

似乎是AutoUnify机制导致它选择了3.0.0.0版本而不是1.0.0.0版本。

根据我对这个错误的了解,如果它是一个 EXE 项目,可以使用文件 app.config 中的 bindingRedirect 来修复它。

我的理解是,这不适用于 DLL 项目,因此不适用于 Visual Studio 扩展 (VSIX) 项目。

有什么方法可以消除 VSIX 项目中的这些警告?

(顺便说一句,我还没有更新到3.0.0.0版本,因为这个版本只针对.NET Standard,所以我认为它与Framework 4.7.2不兼容。)

Is there any way that I can get rid of these warnings in a VSIX project?

重现这三个警告的简单方法是创建一个 vsix 空项目,然后以 PackageReference 格式引用 Microsoft.CodeAnalysis version-1.0.0 包。

构建项目,然后你可以得到相同的警告。

所以我认为你引用的包与带有VS2019相关sdk的vsix项目不兼容。请检查this document,对于VS2019,你应该使用3.0或更高版本。

By the way, I have not updated to version 3.0.0.0, because this version only targets .NET Standard, so I assume it is not compatible with Framework 4.7.2.

有关 .net 标准和 .net 框架之间兼容性的详细信息,请查看 this document

请参阅:使用为 .NET Framework 4.6.1 项目中的那些版本构建的 .NET Standard 库存在多个问题。对于需要使用此类库的 .NET Framework 项目,我们建议您将项目升级到目标 .NET Framework 4.7.2 或更高版本.

实际上,在 .net framework 4.7.2 projects 和更高版本中引用以 .net standard 2.0 为目标的程序集是官方推荐的方式。所以你不必担心。

我建议您将 Microsoft.CodeAnalysis 更新为 3.2.0 version,您可能还需要更新相关软件包,例如 Microsoft.CodeAnalysis.Analyzers,然后这个问题就会消失。

希望对您有所帮助:)