Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Defining Common Assembly Properties for Multi-project .NET Core 2.0 Solution in Visual Studio 2017

0.00/5 (No votes)
9 Sep 2017 1  
Ever wondered how you could change/maintain common assembly properties for every project in a Visual Studio 2017 solution based on .NET Core 2.0 in one place?

Problem

Ever wondered how you could change/maintain common assembly properties for every project in a Visual Studio 2017 solution based on .NET Core 2.0 in one place?

We are talking here about product Product, Product Version, Company, Copyright and any other assembly properties that must have similar values for every assembly in the solution.

These are the assembly properties that used to be specified in AssemblyInfo.cs files of .csproj projects in .NET projects that are not .NET Core ones.

Background

It looks like Microsoft decided to move declarations of these properties from the actual source code of a project into the project declaration itself. Now they are represented as MSBuild properties of the project file that is a build recipe for the project.

Let's assume we have the following solution layout:

Here is how assembly properties can look like within the ProjectA.csproj file if you open it in an XML/text editor:

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

  <PropertyGroup>

    <TargetFramework>netcoreapp2.0</TargetFramework>

    <Authors>The Best of The Company</Authors>
    <Company>The Company</Company>
    <Copyright>Copyright © 2017</Copyright>
    
    <Product>The Solution</Product>
    <ProductName>The Solution Name</ProductName>
    <ProductVersion>1.0.0.0</ProductVersion>
    <AssemblyVersion>1.0.0.0</AssemblyVersion>
    <FileVersion>1.0.0.0</FileVersion>

    <RepositoryType>repositoryType</RepositoryType>
    <PackageLicenseUrl>www.license.url</PackageLicenseUrl>
    <PackageProjectUrl>www.project.url</PackageProjectUrl>
    <PackageIconUrl>www.icon.url</PackageIconUrl>
    <RepositoryUrl>www.repository.url</RepositoryUrl>
    <PackageTags>tag1</PackageTags>
    <PackageReleaseNotes>release notes</PackageReleaseNotes>

    <RootNamespace>TheCompany.TheSolution.ProjectA</RootNamespace>
    <AssemblyName>TheSolution.ProjectA</AssemblyName>

  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" 

    Version="2.0.0" />
  </ItemGroup>

</Project>

We can see and change values of these properties by invoking project Properties view and selecting its Package tab.

Since here we are dealing with multiple MSBuild project files of the solution and MSBuild has this nice feature that allows to import external project into a given project, let's do the following:

  1. We can create a new MSBuild project file on the solution level. Let's call it SolutionInfo.proj
  2. Move the common properties declarations into this file (from all of the existing projects of the solution that need to share these common properties)
  3. Import this SolutionInfo.proj into every project of the solution

Solution

I normally add solution wide items into a solution folder called SolutionItems. So, let's right-mouse-click on the solution node within the Visual Studio Solution Explorer. Select Add -> New Solution Folder and name it as SolutionItems.

Now let's right-mouse-click on the newly created solution folder and select Ad -> New Item -> Tex File. Rename the default text file name as SolutionInfo.proj (yes, including the file name extension).

This is how our solution layout looks right now:

Now let's double-click the SolutionInfo.proj file in the Solution Explorer and add the following content to it:

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

  <PropertyGroup>

    <Authors>The Best of The Company</Authors> 
    <Company>The Company</Company> 
    <Copyright>Copyright © 2017</Copyright> 

    <Product>The Solution</Product> 
    <ProductName>The Solution Name</ProductName> 
    <ProductVersion>1.0.0.0</ProductVersion> 
    <AssemblyVersion>1.0.0.0</AssemblyVersion> 
    <FileVersion>1.0.0.0</FileVersion> 

    <RepositoryType>repositoryType</RepositoryType> 
    <PackageLicenseUrl>www.license.url</PackageLicenseUrl> 
    <PackageProjectUrl>www.project.url</PackageProjectUrl> 
    <PackageIconUrl>www.icon.url</PackageIconUrl> 
    <RepositoryUrl>www.repository.url</RepositoryUrl> 
    <PackageTags>tag1</PackageTags> 
    <PackageReleaseNotes>release notes</PackageReleaseNotes> 

  </PropertyGroup> 

</Project>

Here, we just defined a PropertyGroup that includes all of the assembly properties that we want to share across all the projects of the solution.

Now let's import this project file into every project of the solution while removing all the properties of a solution project that we already defined in the SolutionInfo.proj.

Let's do this for ProjectA as an example.

In the Solution Explorer, select the ProjectA node, right-mouse-click it, select Unload Project.

Now, right-mouse-click the same project node again and select Edit ProjectA.csproj.

Now in the just opened text editor, modify the ProjectA.csproj to look similar to this:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RootNamespace>TheCompany.TheSolution.ProjectA</RootNamespace>
    <AssemblyName>TheSolution.ProjectA</AssemblyName>
  </PropertyGroup>

  <Import Project="..\SolutionInfo.proj" />

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" 

    Version="2.0.0" />
  </ItemGroup>

</Project>

Here, we removed the previously defined shared properties and imported the shared SolutionInfo.proj with all the shared properties that it defines.

Now close the file editor, go to the Solution Explorer and right-mouse-click the unloaded project, select Reload Project.

Repeat similar steps for rest of the solution projects.

Now recompile the solution and verify that all of the built assemblies have expected assembly properties.

Also, make sure that project properties of every project show expected Package fields values.

Now, for example, if you want to update product version for all the projects of the solution, modify it within SolutionInfo.proj file.

History

  • 9th September, 2017: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here