王庆宝 发表于 2024-4-25 13:58:46

dotnet 8 破坏性改动 在 AssemblyInformationalVersionAttribute 添加上 gi

我在一个 WPF 项目里面,在界面显示应用的版本号,更新到 dotnet 8 的 SDK 之后,发现我的界面布局损坏了。本质上这个破坏性改动和 WPF 没有什么关系,是 dotnet 的 SDK 或编译器的破坏性变更,在 AssemblyInformationalVersionAttribute 的 InformationalVersion 属性里面写入了当前的 git 的 commit 提交号
咱可以非常方便写出代码获取到当前应用的版本号信息,如下面代码,我新建了一个简单的 WPF 界面用来显示当前应用的版本号
    <Grid>
      <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
            <Run Text="当前版本号:"/>
            <Run x:Name="AppVersionRun"></Run>
      </TextBlock>
    </Grid>对应的后台 cs 代码如下
    public MainWindow()
    {
      InitializeComponent();

      var assemblyInformationalVersionAttribute = GetType().Assembly.GetCustomAttributes<System.Reflection.AssemblyInformationalVersionAttribute>().First();
      AppVersionRun.Text = assemblyInformationalVersionAttribute.InformationalVersion;
    }在使用 dotnet 8 的 SDK 构建时,无论使用的运行时是 dotnet 6 还是 dotnet 7 等,都会在 InformationalVersion 里面拿到 版本号+commit 格式的字符串,如下图

此变更不仅修改了版本号字符串,还影响了 VisualStudio 的热重载
obj\Debug\net7.0-windows\Xxx.AssemblyInfo.cs (第 17 行): 错误 ENC0003: 更新“属性”需要重启应用程序。如果不想要在 InformationalVersion 里面拿到的版本号信息包含 commit 信息,则可以在 csproj 里面配置 IncludeSourceRevisionInInformationalVersion 属性设置为 false 值,如下面代码
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
    <IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
</PropertyGroup>

</Project>修改之后的重新构建应用运行,可以看到此时界面只显示版本号

如果想要自定义版本号后面添加的信息,可以通过配置 SourceRevisionId 属性。在没有设置 IncludeSourceRevisionInInformationalVersion 为 false 的前提下,设置 SourceRevisionId 属性的值,即可让 SourceRevisionId 设置的内容在 InformationalVersion 属性里面追加到版本号字符串后面
如以下代码
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
    <SourceRevisionId>lindexi</SourceRevisionId>
</PropertyGroup>

</Project>以上代码即可使用 SourceRevisionId 的信息代替默认的 git 的 commit 提交信息,拼接到版本字符串后面
以上代码运行的界面如下

更多请参阅:

[*]MSBuild properties for Microsoft.NET.Sdk - .NET Microsoft Learn
[*]Git commit ID included in assembly ProductVersion field when building with sdk 8 · Issue #34568 · dotnet/sdk
[*]c# - FileVersionInfo.ProductVersion suddenly contains git commit hash - Stack Overflow
本文以上的代码放在github 和 gitee 欢迎访问
可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 21cb92ee87b9903552120695ecd8db785ec9dfff以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin 21cb92ee87b9903552120695ecd8db785ec9dfff获取代码之后,进入 ChedemwoheGelnairkoni 文件夹

来源:https://www.cnblogs.com/lindexi/p/17852947.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: dotnet 8 破坏性改动 在 AssemblyInformationalVersionAttribute 添加上 gi