You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Visual Studio 2026构建报错:'<‘是无效值起始符及关联问题咨询

问题:Visual Studio 2026构建报错“'<‘是无效值起始符”,关联Microsoft.WebTools.Aspire.targets及Azure.Identity包
  • 使用Visual Studio 2026(最新社区版)构建项目时,出现错误:'<‘是无效值起始符。行号:2 | 字节位置:0,指向文件C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VisualStudio\WebTools.Aspire\Microsoft.WebTools.Aspire.targets
  • 项目可正常运行,但涉事项目的编辑器存在异常:鼠标悬停在IEnumerable上时无语法高亮和描述弹窗
  • 重装Windows 11后问题消失一周,现已复现;团队3名开发者中仅本人遇到该问题
  • 项目未引用Aspire,也无自定义构建流程
  • 排查发现:错误提示返回的XML经验证合法,是某程序将该XML文件当作JSON解析;且使用Azure.Identity NuGet包1.20.0及更高版本时会触发此错误

报错高亮的XML代码段:

<GenerateCombinedAppSettingsSchema
  AppSettingJsonSchemaFilePath="$(AppSettingJsonSchemaLoadFilePath)"
  ComponentJsonSchemaCombinedFilePath="$(JsonSchemaCombinedFilePath)"
  AppSettingsJsonSchemaCombinedFilePath="$(AppSettingsJsonSchemaCombinedFilePath)">
</GenerateCombinedAppSettingsSchema>

完整的Microsoft.WebTools.Aspire.targets文件内容:

<?xml version="1.0" encoding="utf-8"?>

<!--
***********************************************************************************************
Microsoft.WebTools.Aspire.targets

WARNING:  DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
          created a backup copy.  Incorrect changes to this file will make it
          impossible to load or build your projects from the command-line or the IDE.

Copyright (C) Microsoft Corporation. All rights reserved.
***********************************************************************************************
-->

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <UsingTask
      TaskName="Microsoft.WebTools.Aspire.MSBuild.GenerateCombinedComponentSchema"
      AssemblyFile="Microsoft.WebTools.Aspire.MSBuild.dll" />
  <UsingTask
      TaskName="Microsoft.WebTools.Aspire.MSBuild.GenerateCombinedAppSettingsSchema"
      AssemblyFile="Microsoft.WebTools.Aspire.MSBuild.dll" />

  <PropertyGroup>
    <JsonSchemaCombinedFilePath>$(IntermediateOutputPath)\CombinedComponentSchema.json</JsonSchemaCombinedFilePath>
    <JsonSchemaCombinedFilePath>$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), $(JsonSchemaCombinedFilePath)))</JsonSchemaCombinedFilePath>
    <AppSettingsJsonSchemaCombinedFilePath>$(IntermediateOutputPath)\AppSettingsSchema.json</AppSettingsJsonSchemaCombinedFilePath>
    <AppSettingsJsonSchemaCombinedFilePath>$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), $(AppSettingsJsonSchemaCombinedFilePath)))</AppSettingsJsonSchemaCombinedFilePath>
    <SchemaGenIncrementalBuildInputs>@(JsonSchemaSegment);$(ProjectAssetsFile)</SchemaGenIncrementalBuildInputs>
  </PropertyGroup>

  <Target Name="GenerateCombinedComponentSchemaTarget"
          Condition=" '@(JsonSchemaSegment)' != '' "
          Inputs="$(SchemaGenIncrementalBuildInputs)"
          Outputs="$(JsonSchemaCombinedFilePath)">
    <GenerateCombinedComponentSchema
      JsonSchemaSegmentFiles="@(JsonSchemaSegment)"
      JsonSchemaCombinedFilePath="$(JsonSchemaCombinedFilePath)">
    </GenerateCombinedComponentSchema>
  </Target>

  <Target Name="GenerateCombinedAppSettingsSchemaTarget"
          DependsOnTargets="GenerateCombinedComponentSchemaTarget"
          Condition=" '@(JsonSchemaSegment)' != '' "
          BeforeTargets="CompileDesignTime"
          Inputs="$(SchemaGenIncrementalBuildInputs);$(JsonSchemaCombinedFilePath)"
          Outputs="$(AppSettingsJsonSchemaCombinedFilePath)">
    <GenerateCombinedAppSettingsSchema
      AppSettingJsonSchemaFilePath="$(AppSettingJsonSchemaLoadFilePath)"
      ComponentJsonSchemaCombinedFilePath="$(JsonSchemaCombinedFilePath)"
      AppSettingsJsonSchemaCombinedFilePath="$(AppSettingsJsonSchemaCombinedFilePath)">
    </GenerateCombinedAppSettingsSchema>
    <Message Text="Generating appsettingsschema" />
  </Target>
  
  <Target Name="CleanupCombinedAppSettingsSchemaTarget"
          Condition=" '@(JsonSchemaSegment)' == '' "
          BeforeTargets="CompileDesignTime">
    <Delete Files="$(AppSettingsJsonSchemaCombinedFilePath)" />
    <Message Text="Deleting appsettingsschema" />
  </Target>
</Project>
解决方案

1. 禁用Aspire相关MSBuild目标

由于项目未引用Aspire,可手动在项目文件(.csproj)中添加配置,阻止加载Aspire的targets文件:

<PropertyGroup>
  <DisableAspireSchemaGeneration>true</DisableAspireSchemaGeneration>
</PropertyGroup>

或者直接排除该targets文件的导入:

<ItemGroup>
  <MSBuildRemoveTargets Include="Microsoft.WebTools.Aspire.targets" />
</ItemGroup>

2. 回退Azure.Identity包版本

暂时将Azure.Identity回退到1.19.0版本,确认是否解决问题:

Install-Package Azure.Identity -Version 1.19.0

后续关注Azure.Identity官方更新,看是否修复了触发Aspire目标的问题。

3. 重置Visual Studio组件缓存

  • 关闭Visual Studio
  • 删除以下目录的缓存文件:
    • %LOCALAPPDATA%\Microsoft\VisualStudio\18.0_*\ComponentModelCache
    • %USERPROFILE%\.nuget\packages\azure.identity(删除对应版本后重新安装)
  • 重新打开Visual Studio并清理重建项目

4. 修复Visual Studio安装

打开Visual Studio Installer,选择“修改”,然后点击“修复”,确保Aspire相关组件安装完整且无损坏。

5. 检查MSBuild属性冲突

查看项目的Directory.Build.propsDirectory.Build.targets文件,确认是否有自定义的JsonSchemaSegmentAppSettingJsonSchemaLoadFilePath属性,这些属性可能触发Aspire的目标执行。

内容的提问来源于stack exchange,提问作者user32518075

火山引擎 最新活动