Home > Software design >  Central location for Version info for .Net Core 6 solution in Visual Studio 2022
Central location for Version info for .Net Core 6 solution in Visual Studio 2022

Time:02-22

For ASP.Net with Visual Studio 2019, I created a shared AssemblyInfo.cs file which was then referenced in each of the projects of the Solution. Thereby giving me one location to set the Version info which would then be set for each project.

using System.Reflection;
using System.Runtime.InteropServices;
using JetBrains.Annotations;

[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Company Name")]
[assembly: AssemblyProduct("Product name")]
[assembly: AssemblyCopyright("Copyright © Company. All Rights Reserved.")]
[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)]

[assembly: AssemblyVersion("1.15.0")]
[assembly: AssemblyFileVersion("1.15.0.7156")]
[assembly: AssemblyInformationalVersion("1.15.0")]

Each project had a simplified AssemblyInfo.cs file:

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("ProjectName")]
[assembly: AssemblyDescription("")]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]

and the shared AssemblyInfo.cs file was included in each project as a Link.

I am starting a new solution using Visual Studio 2022 and .Net Core 6. I see that I can set the Version, AssemblyVersion and FileVersion in the csproj of each project ... but I can't see how I can have a single central place to set the values as I did in the previous solution.

I have seen that I can revert to the manual creation of the AssemblyInfo.cs file, but is there a way to do what I want without disabling the current standard way of doing it (in VS2022 / .Net Core 6)?

The code is under source control (SVN) but we do not (currently) use any build or CI tools. I just want to be able to set the version to a specific value and have it the same for all projects within the solution. I appreciate I could open each csproj file and update them each, but I'd rather have a single location.

CodePudding user response:

You can use MSBuild imports to include a common file in each csproj. For example, create a file with all your shared properties in it:

<Project>
  <PropertyGroup>
    <AssemblyVersion>1.15.0</AssemblyVersion>
    <AssemblyFileVersion>1.15.0.7156</AssemblyFileVersion>
    <!-- etc... -->
  </PropertyGroup>
</Project>

Then you can <Import Project="CommonAssemblyProperties.props" /> (or whatever you name the file) in each project file.

If you want to make the import automatic, you can put it in a Directory.Build.props file at the root of your solution, as this file is automatically imported (you could even assign the properties here, instead of having to <Import> an extra file).

This approach also lets you override property values during the build if you want (e.g. if you want a CI build to be different than a dev build) using msbuild MySolution.sln /p:Property=Value, or in individual project by setting the property value in the csproj (e.g. if one project has a special version different from the others).

  • Related