You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor WebAssembly版本号自定义配置及自动递增实现方法咨询

Blazor WebAssembly 版本号自动递增配置指南

一、版本号核心原理

Blazor WASM的版本信息由MSBuild编译时的三个关键属性控制:

  • AssemblyVersion:程序集版本,影响.NET程序集的强命名(可选)
  • FileVersion:对应Windows文件属性里的版本号
  • InformationalVersion:可包含预发布标签(如1.2.3-beta),也是你当前配置的属性

编译时MSBuild会读取.csproj里的这些属性值,嵌入到输出的程序集和blazor.boot.json中(Blazor会在这个文件里暴露版本信息)。自动递增本质就是让这些属性值在每次编译时动态更新。

二、现有配置优化

你当前通过APPVERSION环境变量控制InformationalVersion的逻辑是可行的,可以保留,在此基础上扩展自动递增逻辑:

<PropertyGroup>
  <!-- 无环境变量时的默认版本 -->
  <InformationalVersion Condition="'$(APPVERSION)' == ''">1.0.0.0</InformationalVersion>
  <!-- 环境变量指定的版本 -->
  <InformationalVersion Condition="'$(APPVERSION)' != ''">$(APPVERSION)</InformationalVersion>
</PropertyGroup>

三、自动递增实现方案

方案1:MSBuild内置自动递增(最简单)

MSBuild支持用*号实现版本号自动递增,适合本地开发快速测试:
修改.csproj的AssemblyVersion和FileVersion(按需配置):

<PropertyGroup>
  <!-- 主版本.次版本.*.*,编译时自动递增修订号和构建号 -->
  <AssemblyVersion>1.0.*</AssemblyVersion>
  <FileVersion>1.0.*</FileVersion>
  <!-- 信息版本结合自动生成的版本号,添加dev标识区分开发版 -->
  <InformationalVersion Condition="'$(APPVERSION)' == ''">$(AssemblyVersion)-dev</InformationalVersion>
  <InformationalVersion Condition="'$(APPVERSION)' != ''">$(APPVERSION)</InformationalVersion>
</PropertyGroup>

原理:MSBuild会将*替换为基于编译时间的递增数值(前两位是日期,后两位是当天的编译次数),每次编译都会自动变化。

方案2:自定义版本文件存储(可控性强)

如果需要更可控的递增逻辑(比如手动控制主/次版本,只递增修订号),可以用本地文件存储当前版本号,编译时读取并递增:

  1. 在项目根目录创建Version.txt文件,写入初始版本:1.0.0.0
  2. 在.csproj中添加MSBuild任务,实现版本读取、递增、写入:
<PropertyGroup>
  <VersionFile>$(ProjectDir)Version.txt</VersionFile>
</PropertyGroup>

<Target Name="UpdateVersion" BeforeTargets="CoreCompile">
  <!-- 读取当前版本号 -->
  <ReadLinesFromFile File="$(VersionFile)">
    <Output TaskParameter="Lines" ItemName="VersionLines" />
  </ReadLinesFromFile>
  <PropertyGroup>
    <CurrentVersion>@(VersionLines)</CurrentVersion>
    <!-- 拆分版本号为各段数值 -->
    <MajorVersion>$([System.Version]::Parse('$(CurrentVersion)').Major)</MajorVersion>
    <MinorVersion>$([System.Version]::Parse('$(CurrentVersion)').Minor)</MinorVersion>
    <BuildVersion>$([System.Version]::Parse('$(CurrentVersion)').Build)</BuildVersion>
    <!-- 递增修订号 -->
    <NewRevision>$([System.Version]::Parse('$(CurrentVersion)').Revision + 1)</NewRevision>
    <NewVersion>$(MajorVersion).$(MinorVersion).$(BuildVersion).$(NewRevision)</NewVersion>
  </PropertyGroup>
  <!-- 将新版本号写回文件 -->
  <WriteLinesToFile File="$(VersionFile)" Lines="$(NewVersion)" Overwrite="true" />
  <!-- 赋值给版本属性 -->
  <PropertyGroup>
    <InformationalVersion Condition="'$(APPVERSION)' == ''">$(NewVersion)</InformationalVersion>
    <AssemblyVersion>$(NewVersion)</AssemblyVersion>
    <FileVersion>$(NewVersion)</FileVersion>
  </PropertyGroup>
</Target>

原理:编译前执行自定义MSBuild任务,读取版本文件拆分数值,递增修订号后写回文件,最后把新版本号赋值给相关属性。

方案3:CI/CD环境自动递增(适合正式发布)

在CI/CD流水线(如GitHub Actions、Azure DevOps)中,可通过流水线变量实现自动递增,无需修改本地配置:
以GitHub Actions为例,用脚本生成递增版本号,传递给APPVERSION参数:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: 生成递增版本号
        id: version
        uses: actions/github-script@v7
        with:
          script: |
            // 读取仓库最新标签,生成递增版本
            let version = '1.0.0.0';
            try {
              const latestTag = await github.rest.git.getRef({
                owner: context.repo.owner,
                repo: context.repo.repo,
                ref: 'tags/latest'
              });
              const [major, minor, build, revision] = latestTag.data.ref.split('/')[2].split('.').map(Number);
              version = `${major}.${minor}.${build}.${revision + 1}`;
            } catch {}
            core.setOutput('version', version);
      - name: 构建Blazor WASM
        run: dotnet publish -c Release /p:APPVERSION=${{ steps.version.outputs.version }}

原理:CI/CD流水线中动态生成递增版本号,通过/p:APPVERSION参数传递给MSBuild,覆盖.csproj里的默认值。

四、验证版本号是否生效

编译完成后,可通过两种方式验证:

  1. 查看输出的wwwroot/blazor.boot.json文件,assemblyName字段会包含版本号
  2. 在Blazor组件中直接读取版本信息:
@using System.Reflection

<p>当前版本:@Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion</p>

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 20:03:34