12

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?

Doc
  • 5,078
  • 6
  • 52
  • 88
  • Possible duplicate of [Equivalent to AssemblyInfo in dotnet core/csproj](http://stackoverflow.com/questions/42138418/equivalent-to-assemblyinfo-in-dotnet-core-csproj) – Christian.K Mar 14 '17 at 15:58
  • it's not. I already know where .net core assembly info are written, I'm asking how to centralize this info for multiple projects – Doc Mar 14 '17 at 16:56
  • I must admit I have not tried it, but have you read the first [answer](http://stackoverflow.com/a/42183749/21567)? It shows how to disable the usage of the information from csproj and thus should allow you to keep using your existing approach (using a SharedAssemblyInfo.cs file). Also see [Hans' Answer](http://stackoverflow.com/a/42793803/21567) which is essentially just that. – Christian.K Mar 15 '17 at 06:03

2 Answers2

19

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.

Eric Erhardt
  • 2,286
  • 15
  • 19
  • 5
    If you want to be selective about which projects include the common ".props" file: name it something different like "CommonAssemblyInfo.props", and then add an `` line at the top of each `.csproj` you want to share the properties. – Eric Erhardt Mar 14 '17 at 18:37
  • @EricErhardt You can also set the `ImportDirectoryBuildTargets` property to false for projects you want to exclude if you prefer the blacklist approach. – Mike E May 10 '17 at 16:53
  • what about [assembly: AssemblyInformationalVersion("a.b.c.d")] - it looks like there is no property for this attribute in net core – Macko Oct 24 '17 at 12:47
  • 1
    @Macko - Check out the `` property. It maps to `System.Reflection.AssemblyInformationalVersionAttribute`. See https://github.com/dotnet/sdk/blob/5d4aa5c712512861c5f0987f6695ca0868192aca/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.GenerateAssemblyInfo.targets#L69-L71 – Eric Erhardt Oct 25 '17 at 14:12
4

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

AeroX
  • 3,387
  • 2
  • 25
  • 39
Uros
  • 2,099
  • 7
  • 23
  • 50