VS构建引用自定义分析器项目的解决方案时出现CS8032警告如何解决?
问题描述
长期可正常编译的项目,几周前开始在Visual Studio中构建解决方案或非分析器类任意项目时,持续出现CS8032警告:
Warning CS8032 An instance of analyzer Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer cannot be created from D:\Dev\Repos\Azure\Platform\src\Shared\Analyzers\Platform.Shared.Analyzers\bin\Debug\net5.0\Platform.Shared.Analyzers.dll: Exception has been thrown by the target of an invocation
关键信息
- 所有类库均使用.NET 5版本,尝试切换至.NET 6预览版后警告仍存在
- 项目通过Directory.Build.props文件引用分析器项目,配置如下:
<ItemGroup> <ProjectReference Include="$(MSBuildThisFileDirectory)\Shared\Analyzers\Platform.Shared.Analyzers\Platform.Shared.Analyzers.csproj" PrivateAssets="all" ReferenceOutputAssembly="false" OutputItemType="Analyzer"/> </ItemGroup>
已尝试的排查操作
- 使用VS 16.10版本构建:仍出现警告
- 升级至VS 16.11版本构建:仍出现警告
- 安装VS 17预览版(2022)并升级至.NET 6构建:仍出现警告
- 在PowerShell中执行
dotnet build、dotnet msbuild构建:无警告 - 使用JetBrains Rider构建:无警告
- 在Azure DevOps构建代理中构建:无警告
完整错误日志
开启Fusion Log获取的完整错误信息如下:
Warning CS8032 An instance of analyzer Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer cannot be created from D:\Dev\Repos\Azure\Platform\src\Shared\Analyzers\Platform.Shared.Analyzers\bin\Debug\net5.0\Platform.Shared.Analyzers.dll: Exception has been thrown by the target of an invocation.. Platform.Shared.Core 1 Active System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. at Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer..cctor() --- End of inner exception stack trace --- at Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer..ctor() --- End of inner exception stack trace --- at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at Microsoft.CodeAnalysis.Diagnostics.AnalyzerFileReference.Extensions`1.GetAnalyzersForTypeNames(Assembly analyzerAssembly, IEnumerable`1 analyzerTypeNames, Boolean& reportedError) ----- System.TypeInitializationException: The type initializer for 'Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. at Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer..cctor() --- End of inner exception stack trace --- at Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer..ctor() ----- Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. ----- === Pre-bind state information === LOG: DisplayName = System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a (Fully-specified) LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/ LOG: Initial PrivatePath = NULL Calling assembly : Platform.Shared.Analyzers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. === LOG: This bind starts in LoadFrom load context. WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load(). LOG: Using application configuration file: C:\Users\Shay\AppData\Local\Microsoft\VisualStudio\16.0_0bee0d9b\devenv.exe.config LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/System.Runtime/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PublicAssemblies/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PublicAssemblies/System.Runtime/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/System.Runtime/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/System.Runtime/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/System.Runtime/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/System.Runtime/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Runtime/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/System.Runtime/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PublicAssemblies/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PublicAssemblies/System.Runtime/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/System.Runtime/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/System.Runtime/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/System.Runtime/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/System.Runtime/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Runtime/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Users/Shay/AppData/Local/Temp/VS/AnalyzerAssemblyLoader/2dfa9a6f85874e39aff972af6188cb73/5/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Users/Shay/AppData/Local/Temp/VS/AnalyzerAssemblyLoader/2dfa9a6f85874e39aff972af6188cb73/5/System.Runtime/System.Runtime.DLL. LOG: Attempting download of new URL file:///C:/Users/Shay/AppData/Local/Temp/VS/AnalyzerAssemblyLoader/2dfa9a6f85874e39aff972af6188cb73/5/System.Runtime.EXE. LOG: Attempting download of new URL file:///C:/Users/Shay/AppData/Local/Temp/VS/AnalyzerAssemblyLoader/2dfa9a6f85874e39aff972af6188cb73/5/System.Runtime/System.Runtime.EXE.
当前临时解决方案
在解决方案的Directory.Build.props文件中添加如下配置,临时屏蔽该警告:
<PropertyGroup Condition="'$(BuildingInsideVisualStudio)' == 'true'"> <!-- Visual Studio compiler displays the CS8032 warning for unknown reason. Removing it manually to declutter the Error List --> <NoWarn>$(NoWarn),8032</NoWarn> </PropertyGroup>
根本原因
Visual Studio的Roslyn分析器宿主运行在.NET Framework 4.x环境下,而当前分析器项目目标框架为net5.0,其依赖的System.Runtime 5.0.0.0是.NET 5专属程序集,在.NET Framework运行环境中不存在对应版本的程序集,因此加载分析器实例失败触发CS8032警告。
dotnet build、Rider、Azure DevOps代理等构建环境使用的是.NET Core/.NET 5+的MSBuild宿主,可正常识别加载.NET 5程序集,因此不会出现该警告。
正式解决方案
修改分析器项目Platform.Shared.Analyzers.csproj的目标框架为netstandard2.0,该版本可同时兼容.NET Framework和.NET Core/.NET 5+运行环境,完全适配Visual Studio的分析器宿主:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <!-- 将原TargetFramework的值从net5.0改为netstandard2.0 --> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> <!-- 其余配置保持不变 --> </Project>
若分析器引用了Microsoft.CodeAnalysis相关NuGet包,需确保包版本不高于所用Visual Studio版本支持的Roslyn版本:VS 2019 16.10+对应Roslyn版本为3.10.x,引用的CodeAnalysis包版本不要超过该范围即可。
修改完成后重新编译分析器项目,再构建解决方案即可消除CS8032警告,无需再通过NoWarn屏蔽。
内容的提问来源于stack exchange,提问作者Shay

