51

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?

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61
Nathan
  • 675
  • 2
  • 9
  • 20

5 Answers5

65

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.

poke
  • 369,085
  • 72
  • 557
  • 602
16

According to migration guide, these days, we have few flexible ways to set up assembly attributes.

  1. Use old-style AssemblyInfo.cs file (create manually).
using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("...")]
[assembly: System.Reflection.AssemblyCopyrightAttribute("...")]
[assembly: System.Reflection.AssemblyDescriptionAttribute("...")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("...")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("...")]
[assembly: System.Reflection.AssemblyProductAttribute("...")]
[assembly: System.Reflection.AssemblyTitleAttribute("...")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0-dev01234567")]
  1. Use MSBuild's properties to generate assembly attributes on build time (may be static in your.csproj or passed via command line arguments like /property:Version=1.0.0-dev01234567).
<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <Company>...</Company>
        <Copyright>...</Copyright>
        <Description>...</Description>
        <Product>...</Product>
        <AssemblyTitle>...</AssemblyTitle>
        <Version>1.0.0-dev01234567</Version>
    </PropertyGroup>

    ...
</Project>

Note: you may merge both solutions but avoid duplicates of assembly attributes.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Maksym Anurin
  • 3,167
  • 1
  • 18
  • 13
14

I'm using Visual Studio 2022 and .NET Core 6.

  1. Right click on the project in Solution Explorer
  2. Edit Project File

Here is an example project file containing Version, AssemblyVersion and FileVersion:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <Version>1.0.0</Version>
    <AssemblyVersion>1.0.0.0</AssemblyVersion>
    <FileVersion>1.0.0.0</FileVersion>
  </PropertyGroup>
  ...
</Project>
datchung
  • 3,778
  • 1
  • 28
  • 29
9
  1. From Your Project -> Select Properties
  2. On left panel select Package/General
  3. At field Assembly version and File version enter your version (ex: 1.0.0.0)

enter image description here

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
hieuvoquoc
  • 91
  • 1
  • 2
  • 1
    This is confusing in Visual Studio 2022 and .Net 7. The Package > General Section start with a check box - Generate NuGet package on build operations. This helped me to understand they are indeed the Assembly Version and File Version with or without Nuget – JClarkCDS Jan 19 '23 at 00:35
7

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.
Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61