Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the assembly version for a .NET Core project?

Tags:

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?

like image 561
Nathan Avatar asked Oct 17 '19 13:10

Nathan


People also ask

Where do I put assembly version?

You can set the assembly version using the AssemblyVersionAttribute. Assembly attributes are usually applied in the AssemblyInfo.

How can you get the assembly version in C#?

I can get the Assembly Version with the following line of code: Version version = Assembly.

What is assembly version in C#?

It's the version number used by framework during build and at runtime to locate, link, and load the assemblies. When you add reference to any assembly in your project, it's this version number that gets embedded.

What is the order of an assembly version?

The AssemblyVersion attribute assigns the version number of the assembly, and this is embedded in the manifest. Version information for an assembly consists of the following four values : a major and minor version number, and two further optional build and revision numbers.


2 Answers

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.

like image 120
poke Avatar answered Oct 16 '22 10:10

poke


Reasons for still using an AssemblyInfo.cs file might include

  1. you want to share some of the AssemblyInfo across projects, which you can do with a file
  2. you might have a code-generation process that spits out the assemblyinfo
  3. the project file format doesn't yet support all the attributes you might want to use. The project Sdk knows how to auto-generate a limited set of [AssembyAttributes] from Xml Elements with matching names in the csproj file, but it doesn't support autogeneration of arbitrary [AssembyAttributes] or other metadata for your assembly.
  4. AssemblyInfo.cs is “just” a source code file, you might have other metadata – whether AssemblyAttributes or classes or other – you want to keep all in one easily found place.
like image 30
Chris F Carroll Avatar answered Oct 16 '22 09:10

Chris F Carroll