SharedAssemblyInfo equivalent in .net core project

2020-06-16 04:05发布

问题:

When I wanted multiple projects in a solution to share the same assembly informations (mainly AssemblyVersion), I used to use the tecnique explained here: https://blogs.msdn.microsoft.com/jjameson/2009/04/03/shared-assembly-info-in-visual-studio-projects/

Now we're working on a solution of .net core and asp.net core projects and we want all projects to share the same AssemblyVersion, but now each project's version is stored inside it's .csproj file (we're using vs2017)

I tried with generating info with <GenerateAssemblyProductAttribute>true</GenerateAssemblyProductAttribute> but the new file is created at build-time

Is there a way to centralize this information like in the "old" SharedAssemblyInfo.cs way?

回答1:

You can add a Directory.Build.props file above all the .csproj files that you want to default properties for. This .props file will automatically be imported in all the .csproj files below it. You can set common properties in it:

<Project>
  <PropertyGroup>
    <Company>Erhardt Enterprises</Company>
    <Copyright>Erhardt Enterprises ©</Copyright>
  </PropertyGroup>
</Project>

And these MSBuild properties will apply to all the .csproj files under it. So when the assembly attributes are generated, these values will be used across all projects.

Here is the mapping of all MSBuild properties to assembly attributes.



回答2:

You can create .props file (for example common.props) and put common project configuration in this file:

common.props:

<Project>
  <PropertyGroup>
    <AspNetCoreVersion>2.0.0</AspNetCoreVersion>
  </PropertyGroup>
</Project>

project1.csproj:

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

  <Import Project=".\common.props" />

  ...

</Project>

project2.csproj:

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

  <Import Project=".\common.props" />

  ...

</Project>

You can find more info on this link