Click here to Skip to main content
15,890,882 members
Articles / .NET / .NET Core
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
9 Sep 2017CPOL3 min read 19.8K   5   3
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:

XML
<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:

XML
<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:

XML
<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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Wide Spectrum Computing LLC
United States United States
Freelance/independent software development/consulting experience.
Over 15 years of software development expertise: small- to large-scale projects.
Ability to lead small groups of developers.
GUI, console, multi-tier, web, service and embedded applications.
Application domain modeling. Domain Driven Design.
Distributed telemetry systems.
Line of Business applications. Amazon MWS API.
Physical and mathematical modeling.
Scientific, medical, and semiconductor instrumentation.
Mission-critical telecommunication applications.

Comments and Discussions

 
PraiseMy vote of 5 Pin
Sergey Alexandrovich Kryukov25-Apr-21 6:24
mvaSergey Alexandrovich Kryukov25-Apr-21 6:24 
QuestionIgnore SDK in project root Pin
Fakhrulhilal Maktum14-Feb-19 20:55
Fakhrulhilal Maktum14-Feb-19 20:55 
QuestionAn alternative Pin
cocowalla18-Sep-17 4:11
cocowalla18-Sep-17 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.