Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you share version information between .NET Standard libraries?

In a typical .NET app, product and version information are stored in the AssemblyInfo.cs file under the 'Properties' folder, like so...

DLL Project
 - Properties Folder
    - AssemblyInfo.cs

In our case, we have a solution where we need to keep version information synced across eleven DLLs.

To accomplish this, we first removed any shared values from each project's AssemblyInfo.cs file, leaving only those values specific to that particular project.

We then placed all of the shared values in a second file called AssemblyInfo_Shared.cs which we store in a sibling folder to those of the projects. We then add that file to each project's 'Properties' folder via a link (that's the little 'down' arrow on the button when you're adding it.)

By doing this, all related DLLs share the same version information while still retaining assembly-specific properties as well. It makes keeping a set of versioned DLLs in sync a piece of cake as we edit a single file and all eleven DLLs' versions update at once.

Here's what it looks like...

Common Folder
  - AssemblyInfo_Shared.cs (Actual)

DLL Project A
 - Properties Folder
    - AssemblyInfo.cs // Only values specific to A
    - AssemblyInfo_Shared.cs (Link)

DLL Project B
 - Properties Folder
    - AssemblyInfo.cs // Only values specific to B
    - AssemblyInfo_Shared.cs (Link)

The contents of AssemblyInfo.cs in Project A looks like this...

using System.Reflection;

[assembly: AssemblyTitle("SomeApp.LibA")]
[assembly: AssemblyDescription("This is the code for  A")]

This is project B's

using System.Reflection;

[assembly: AssemblyTitle("SomeApp.LibB")]
[assembly: AssemblyDescription("This is the code for Project B")]

And here's the shared...

using System;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;

[assembly: AssemblyProduct("SomeApp")]
[assembly: AssemblyVersion("1.4.3.0")]
[assembly: AssemblyFileVersion("1.4.3.0")]
[assembly: AssemblyCompany("MyCo")]
[assembly: AssemblyCopyright("Copyright (c) 2010-2018, MyCo")]
[assembly: ComVisible(false)]
[assembly: NeutralResourcesLanguage("en-US")]

[assembly: CLSCompliant(true)]

With the above, both DLLs share the same version information since the common file is 'merged' with the project-specific one when building. Hope this makes sense.

However, in .NET Standard projects, it looks like the version information is baked right into the Project file under the <PropertyGroup> section, so I'm not sure how we can achieve the same capability.

Does .NET Standard have anything that supports this?

like image 639
Mark A. Donohoe Avatar asked Aug 31 '18 05:08

Mark A. Donohoe


2 Answers

Yes, you can ;o)

Add a folder named Properties to each project root and add the link to AssemblyInfo_Shared.cs.

After that you have to set in each .Net Standard/Core project configuration

<PropertyGroup>
  ...
  <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

You will find a complete Solution at https://github.com/SirRufo/SharedVersionTest

like image 172
Sir Rufo Avatar answered Oct 21 '22 22:10

Sir Rufo


Sir Rufo's solution is perfectly fine, but here's another way, made possible by the new project format. This lets you customize automatic assembly generation.

You can put a Directory.Build.props file in a parent directory of your projects, and it will be automatically included. See the documentation:

However, now you can add a new property to every project in one step by defining it in a single file called Directory.Build.props in the root folder that contains your source. When MSBuild runs, Microsoft.Common.props searches your directory structure for the Directory.Build.props file (and Microsoft.Common.targets looks for Directory.Build.targets). If it finds one, it imports the property. Directory.Build.props is a user-defined file that provides customizations to projects under a directory.

So you can leverage that to add your assembly info:

<?xml version="1.0" encoding="utf-8"?>
<Project>
  <PropertyGroup>
    <Version>1.4.3.0</Version>
    <Product>SomeApp</Product>
    <Company>MyCo</Company>
    <Copyright>Copyright (c) 2010-2018, MyCo</Copyright>
  </PropertyGroup>
</Project>

There's probably no tag for the CLSCompliant attribute, but you'll have everything you need for the assembly info, and some other stuff. Here are the docs for NuGet-related properties, but unfortunately, right now the only "documentation" for assembly info properties is the source code here (until this issue is resolved, at least).

You can then override any property you want in each of your projects.

like image 5
Lucas Trzesniewski Avatar answered Oct 21 '22 20:10

Lucas Trzesniewski