.NET Core/Standard 自动递增版本号





5.00/5 (4投票s)
如何在你的 .NET Core/Standard 项目中获得漂亮的自动递增程序集版本号
使用自动生成的 .NET Core/Standard AssemblyInfo.cs
当你创建一个新的 .NET Core/.NET Standard 项目时,你将获得一组基于项目这些设置的自动生成的属性。
你可以在 obj 文件夹中看到这些属性,其中会为你创建一个自动生成的 xxxxAssemblyInfo.cs 类。
/------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("SomeDemoCode.Std")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("SomeDemoCode.Std")]
[assembly: System.Reflection.AssemblyTitleAttribute("SomeDemoCode.Std")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.
因此,当我们构建项目时,最终会将此版本应用于项目的输出。
好的,这解释了 AssemblyInfo
在 .NET Core/Standard 中的工作方式,但是 .NET Core/Standard 自动递增版本号的方法是什么?
好吧,信不信由你,并没有为此提供原生功能,有很多相关的努力/尝试,你可以阅读更多信息
- https://stackoverflow.com/questions/43019832/auto-versioning-in-visual-studio-2017-net-core
- https://andrewlock.net/version-vs-versionsuffix-vs-packageversion-what-do-they-all-mean/
在阅读了所有这些之后,最佳方法似乎是坚持使用自动生成的 Assembly.info
,但要提出一个方案,以便在构建时生成程序集版本。
这意味着你希望确保你的 .csproj 文件看起来像这样,其中可以看到一些 .NET Core/Standard 自动生成的 AssemblyInfo
内容直接在项目文件中可用
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Platforms>x64</Platforms>
</PropertyGroup>
<PropertyGroup>
<DefineConstants>STD</DefineConstants>
<PlatformTarget>x64</PlatformTarget>
<AssemblyName>SomeDemoCode.Std</AssemblyName>
<RootNamespace>SomeDemoCode.Std</RootNamespace>
<VersionSuffix>1.0.0.$([System.DateTime]::UtcNow.ToString(mmff))</VersionSuffix>
<AssemblyVersion Condition=" '$(VersionSuffix)' == '' ">0.0.0.1</AssemblyVersion>
<AssemblyVersion Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</AssemblyVersion>
<Version Condition=" '$(VersionSuffix)' == '' ">0.0.1.0</Version>
<Version Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</Version>
<Company>SAS</Company>
<Authors>SAS</Authors>
<Copyright>Copyright © SAS 2020</Copyright>
<Product>Demo 1.0</Product>
</PropertyGroup>
</Project>
有了这个,你将获得使用纯 .NET Core/Standard 方法自动版本化的程序集版本。
但是关于 InternalsVisibleTo 呢?
我们经常希望仍然将我们的 .NET Standard 项目暴露给测试项目。如果 .NET Core/Standard 项目自动生成基于默认值或实际 .csproj 文件中的属性的 AssemblyInfo
,我们如何添加 InternalsVisibleTo
,这似乎没有包含在为 .NET Core/Standard 项目创建的自动生成的 AssemblyInfo
中,也没有作为 csproj 级别的 MSBUILD
属性项可用。那么我们该怎么做?
幸运的是,这很简单。我们只需要在自定义文件中执行以下操作,你可以随意命名它,甚至可以将其命名为“AssemblyInfo.cs”。
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("SomeDemoCode.IntegrationTests")]
退出自动程序集信息生成过程并使用我们自己的自动递增方案
如果你想使用 .NET Framework 方法进行自动版本控制,通常是在
[assembly: AssemblyVersion("1.0.*")]
所以你可能会想,嗯,我可以通过添加我自己的 AssemblyInfo.cs 来覆盖它。但这行不通。你会在构建时得到这个
> Error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute
> Error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute
> Error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute
幸运的是,我们可以退出此自动生成过程,我们需要将以下内容添加到我们的 .NET Core/.NET standard csproj 文件中
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Deterministic>false</Deterministic>
</PropertyGroup>
你需要 deterministic 属性,否则你会得到这个错误
Wildcards are only allowed if the build is not deterministic,
which is the default for .Net Core projects. Adding False to csproj fixes the issue.
有了这个,我们现在可以包含一个自定义 AssemblyInfo.cs,例如,可以使用自动递增的版本号,其中我们在指定 AssemblyVersion
时使用通配符。
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SomeDemoCode.Std")]
[assembly: AssemblyDescription("SomeDemoCode .NET Standard")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("SAS")]
[assembly: AssemblyProduct("SAS 1.0")]
[assembly: AssemblyCopyright("Copyright © SAS 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("CA7543D7-0F0F-4B48-9398-2712098E9324")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")]
现在当你构建时,你将在你的 .NET Core/Standard 项目中获得一些漂亮的自动递增程序集版本号。