如何为 .NET Core 项目指定程序集版本? [英] How to specify the assembly version for a .NET Core project?

查看:118
本文介绍了如何为 .NET Core 项目指定程序集版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在新的 .NET Core 项目中没有创建 AssemblyInfo.cs 文件.我已经看到您仍然可以设置程序集属性,例如 AssemblyVersion 等.

是否仍有使用 AssemblyInfo.cs 文件的正当理由?

解决方案

您完全可以像过去一样创建 AssemblyInfo.cs 文件并配置程序集.当然,由于属性是使用程序集属性设置的,因此您不需要使用AssemblyInfo,而是可以选择任何其他文件名,甚至可以选择现有的文件名.

话虽如此,AssemblyInfo.cs 不再包含在默认模板中的原因是新的 SDK 风格的项目类型支持在 csproj 中设置此信息代码> 项目文件.

因此,设置程序集版本的常用方法是在项目文件中设置 Version 属性(或将其自动设置为构建过程的一部分).例如:

<属性组><TargetFramework>netcoreapp3.0</TargetFramework><版本>1.2.3</版本></PropertyGroup>…</项目>

由于这是一个 MSBuild 属性,您也可以在构建过程中设置它,例如使用 dotnet build/p:Version=1.2.3.

还有属性 VersionPrefixVersionSuffix 可用于从环境中自动构建版本号(例如 Git 提交 ID 或构建号).

除了版本相关的属性,还有您可以在项目文件中设置更多 NuGet 属性,这使得 AssemblyInfo.cs 大部分是多余的.

I noticed in new .NET Core projects there is no AssemblyInfo.cs file created. I have seen that you can still set assembly attributes such as AssemblyVersion and so forth.

Are there still any valid reasons to use an AssemblyInfo.cs file?

解决方案

You can absolutely create an AssemblyInfo.cs file and configure your assembly like you did in the past. Of course, since the properties are set using assembly attributes, you do not need to use AssemblyInfo but can choose any other file name or even an existing one.

That being said, the reason that the AssemblyInfo.cs is no longer included in the default templates is that the new SDK-style project type supports setting this information within the csproj project file.

So the usual approach to setting the version of your assembly would be to set the Version property within your project file (or have that automatically set as part of your build process). For example:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <Version>1.2.3</Version>
  </PropertyGroup>

  …
</Project>

Since this is a MSBuild property, you can also set this during the build process e.g. with dotnet build /p:Version=1.2.3.

There are also the properties VersionPrefix and VersionSuffix which can be used to automatically construct version numbers from the environment (e.g. Git commit ids, or build numbers).

In addition to the version related properties, there are also some more NuGet properties you can set in the project file, which makes the AssemblyInfo.cs mostly redundant.

这篇关于如何为 .NET Core 项目指定程序集版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆