Equivalent to AssemblyInfo in dotnet core/csproj

147,368

Solution 1

As you've already noticed, you can control most of these settings in .csproj.

If you'd rather keep these in AssemblyInfo.cs, you can turn off auto-generated assembly attributes.

<PropertyGroup>
   <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup> 

If you want to see what's going on under the hood, checkout Microsoft.NET.GenerateAssemblyInfo.targets inside of Microsoft.NET.Sdk.

Solution 2

Those settings has moved into the .csproj file.

By default they don't show up but you can discover them from Visual Studio 2017 in the project properties Package tab.

Project properties, tab Package

Once saved those values can be found in MyProject.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <Version>1.2.3.4</Version>
    <Authors>Author 1</Authors>
    <Company>Company XYZ</Company>
    <Product>Product 2</Product>
    <PackageId>MyApp</PackageId>
    <AssemblyVersion>2.0.0.0</AssemblyVersion>
    <FileVersion>3.0.0.0</FileVersion>
    <NeutralLanguage>en</NeutralLanguage>
    <Description>Description here</Description>
    <Copyright>Copyright</Copyright>
    <PackageLicenseUrl>License URL</PackageLicenseUrl>
    <PackageProjectUrl>Project URL</PackageProjectUrl>
    <PackageIconUrl>Icon URL</PackageIconUrl>
    <RepositoryUrl>Repo URL</RepositoryUrl>
    <RepositoryType>Repo type</RepositoryType>
    <PackageTags>Tags</PackageTags>
    <PackageReleaseNotes>Release</PackageReleaseNotes>
  </PropertyGroup>

In the file explorer properties information tab, FileVersion is shown as "File Version" and Version is shown as "Product version"

Solution 3

I do the following for my .NET Standard 2.0 projects.

Create a Directory.Build.props file (e.g. in the root of your repo) and move the properties to be shared from the .csproj file to this file.

MSBuild will pick it up automatically and apply them to the autogenerated AssemblyInfo.cs.

They also get applied to the nuget package when building one with dotnet pack or via the UI in Visual Studio 2017.

See https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

Example:

<Project>
    <PropertyGroup>
        <Company>Some company</Company>
        <Copyright>Copyright © 2020</Copyright>
        <AssemblyVersion>1.0.0.1</AssemblyVersion>
        <FileVersion>1.0.0.1</FileVersion>
        <Version>1.0.0.1</Version>
        <!-- ... -->
    </PropertyGroup>
</Project>

Solution 4

You can always add your own AssemblyInfo.cs, which comes in handy for InternalsVisibleToAttribute, CLSCompliantAttribute and others that are not automatically generated.

Adding AssemblyInfo.cs to a Project

  1. In Solution Explorer, right click on <project name> > Add > New Folder.

Add New Folder

  1. Name the folder "Properties".

Name folder Properties

  1. Right click on the "Properties" folder, and click Add > New Item....

Add New Item

  1. Select "Class" and name it "AssemblyInfo.cs".

Name file AssemblyInfo.cs

Suppressing Auto-Generated Attributes

If you want to move your attributes back to AssemblyInfo.cs instead of having them auto-generated, you can suppress them in MSBuild as natemcmaster pointed out in his answer.

Solution 5

Adding to NightOwl888's answer, you can go one step further and add an AssemblyInfo class rather than just a plain class:

enter image description here

Share:
147,368

Related videos on Youtube

hultqvist
Author by

hultqvist

Updated on October 26, 2021

Comments

  • hultqvist
    hultqvist over 2 years

    Since dotnet core moved back to the .csproj format, there is a new autogenerated MyProject.AssemblyInfo.cs which contains, among others:

    [assembly: AssemblyCompany("MyProject")]
    [assembly: AssemblyVersion("1.0.0.0")]
    

    Note that this is automatically regenerated every build. Previously, the file was found in the /obj/ directory, now it appears to be only in memory as the file can't be found on disk and clicking the error message does not open any file.

    This is the error message: enter image description here

    Since they are defined there, I can't define them myself in the classical AssemblyInfo.cs.

    Where/how can I define the Company and Version of a project?

    • Jim Aho
      Jim Aho almost 6 years
      Note that this is not strictly related to dotnet core. This is rather related to the new .csproj based format. It's perfectly fine to use this new .csproj format with targeting the old .NET Framework, for example net461
  • ventiseis
    ventiseis about 7 years
    The settings in the project properties seem to be missing if my project type is Class Library (.NET Standard). Do you have any idea why? I'm using Version 15.1, Release 26403.7, Community Edition.
  • Ivaylo Slavov
    Ivaylo Slavov about 7 years
    Glad to see that I can turn this thing off. Call me old-fashioned, but I prefer the good old AssemblyInfo.cs file than the autogenerated stuff of .netcore. Besides I use external tooling to manage my versions and the contents of the other AssembyInfo entries. I tried to use a custom target to keep my properties out of the project itself but it got me choked for a while.
  • tofutim
    tofutim almost 7 years
    I'm using Class Library (.NET Standard) and see it in the Packages tab. Do you see it there? Once you "Save" something other than the defaults, it will show up in the csproj.
  • natemcmaster
    natemcmaster almost 7 years
    NuGet doesn't read AssemblyInfo.cs. You still have to use MSBuild properties to define the NuGet package version.
  • Shubhan
    Shubhan almost 7 years
    when the file is auto generated, how to set the InternalsVisibleTo attribute in the new csproj format?
  • natemcmaster
    natemcmaster almost 7 years
    @Shubhan this isn't one of the auto-generated attributes. Create an empty .cs file somewhere in your project and add the InternalsVisibleTo code to it
  • VJ2013
    VJ2013 over 6 years
    We ran into an issue with this, when out .NET Core app was referencing projects that use the <GenerateAssemblyInfo>false</GenerateAssemblyInfo> setting. stackoverflow.com/questions/48343681/…
  • Junius
    Junius almost 6 years
    Thanks NightOwl888, this is the answer that I'm looking for.
  • Igor Mironenko
    Igor Mironenko over 5 years
    I would avoid assuming everyone has Visual Studio these days, there are other editors that could be used making this answer difficult to follow for some (eg I'm doing this on a Mac/Mono using Jetbrains Rider)
  • Igor Mironenko
    Igor Mironenko over 5 years
    If you put a Directory.build.props file at the root of your solution and add the required attributes - there are not many good reasons to cling to using AssemblyInfos for version info etc. Even if you have legacy tooling (as I do). Admittedly, I still use AssemblyInfo for InternalsVisibleTo
  • Soenhay
    Soenhay over 5 years
    How do you use a wildcard like 1.0.*.* when using the packages tab?
  • Justin
    Justin about 5 years
    I added Directory.Build.props to the root of my solution, updated it to set my Company, Product, and Copyright, then built the solution. When I open any of the projects, they do not have the correct values for those three fields. What am I missing?
  • pfx
    pfx about 5 years
    @Justin, you won't see them within your project files; they get applied on the resulting built assemblies.
  • Joe Phillips
    Joe Phillips about 5 years
    What about those of us who do not use msbuild?
  • T.S.
    T.S. about 5 years
    This is nice. But there are other scenarios, when extremely large, multi-solution build, with some projects .Net Standard, others - .Net FW/different versions, etc.? When third party tools do the versioning, and some other, additional and custom properties are set by them.
  • GKS
    GKS almost 5 years
    @Soenhay, wildcarding doesn't make much sense when defining the package version, only when consuming it.
  • hultqvist
    hultqvist almost 5 years
    @Soenhay my understanding is that you can't unless you use similar feature in third party tools.
  • SwissCoder
    SwissCoder over 4 years
    There is no "Assembly Information File" when I am open this dialog in VS2019 for a netstandard 1.1 Project.
  • justdan23
    justdan23 almost 4 years
    Sometimes, new Microsoft leads should consider keeping what works well with AssemblyInfo.cs so automated builds can still work to modify the build numbers.
  • justdan23
    justdan23 almost 4 years
    Thanks for posting this! I am using .NET Core 3.1 and it was right there! It adds all the key default parts.
  • BittermanAndy
    BittermanAndy over 3 years
    @PandaWood there are many good reasons to "cling to" AssemblyInfos. They're easier to share and edit for a start. If it's a new year and I need to change the date on my Copyright attribute, or if I decide to change the text in the Company attribute, etc., I do NOT want to have to open hundreds of projects or dozens of solutions individually to do it, and I don't want to have to hand-edit .csproj files either. With AssemblyInfo it was easy - Shared.AssemblyInfo.cs, added as a link in the IDE UI. Every hack to workaround the new way of doing things is harder than that and is a waste of dev time.
  • Carsten Führmann
    Carsten Führmann over 3 years
    InternalsVisibleTo can also be put into the "csproj" file, see here: stackoverflow.com/questions/42810705/….
  • hultqvist
    hultqvist almost 3 years
    The answer doesn't explain how it addresses the questions.
  • Mariusz Pawelski
    Mariusz Pawelski almost 3 years
    @BittermanAndy But @PandaWood already told you you can use Directory.build.props which has more advantage than your solution because you don't have to modify every project to add this Shared.AssemblyInfo.cs file as link. You don't have to teach developers that they should add it when they create new project and check it on code review. It just works for all current and new projects automatically. How doing nothing is "harder" than having to remember to modify csproj.
  • sommmen
    sommmen over 2 years
    Any msdn / doc / other resources about this?
  • OutOFTouch
    OutOFTouch over 2 years
    @MariuszPawelski Can you point at how to dynamically change copyright year with dotnet core way of doing AssemblyInfo?
  • Sameer
    Sameer about 2 years
    And if you're like me looking to avoid creating your own AssemblyInfo.cs just for InternalsVisibleToAttribute there is a way as @meziantou wrote in his blog post meziantou.net/declaring-internalsvisibleto-in-the-csproj.htm
  • NightOwl888
    NightOwl888 about 2 years
    @Sameer - Thanks. Actually, we do use that approach, except for we have further improved it by having it auto-generate the PublicKey for strongly named assemblies if the option is enabled: github.com/apache/lucenenet/blob/…. It wasn't working for CLSCompliant, though, because it quoted the value automatically. See: github.com/dotnet/msbuild/issues/2281. Looks like that may have been fixed now, also.
  • Xan-Kun Clark-Davis
    Xan-Kun Clark-Davis about 2 years
    Perfect solution for my case! Thx :-)
  • Kenneth Evans
    Kenneth Evans almost 2 years
    Worked for me for .Net 6 and VS 2022.
  • Kenneth Evans
    Kenneth Evans almost 2 years
    It also worked when those lines were in the .csproj file, which seems a more elegant solution.