I have developed a nuget package in which I need to obtain the package version of the entry assembly. So far, I used this method to get the version:
using System.Reflection;
var version = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
Console.WriteLine(version);
It was working well and was returning only the package version of the entry assembly until now. But there was a breaking change in InformationalVersion
and now the SourceRevisionId is appended to InformationalVersion. For instance, instead of getting 1.2.3-alpha
as expected, I get 1.2.3-alpha+9d1761b41223159ec45d7d492e08820f706d7ad1
.
I know it is possible to add <IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
in the csproj of the entry assembly as suggested here : FileVersionInfo.ProductVersion suddenly contains git commit hash
But this solution is not suitable to me because my package is used by multiple clients and I don't want to have to modify all of them. I need a way to get only the package version without the added SourceRevisionId.
Is there a proper way to do this or do the only solution is to truncate the string after the "+" sign ?
Currently, modifying the .csproj is the only way to resolve the git hash being assigned to the assembly version. As a workaround, you could simply change your code to retrieve the version to include the following:
using System.Linq;
using System.Reflection;
var version = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
version = version.Split("+").First();
Console.WriteLine(version);
The above code will split off the git hash from the version and grab just the first part of the string after the "+".
However, I do question why you can't change the .csproj - you should be able to change that and then push out the updated package to your clients without any manual update of each client.