Reputation: 876
I have read several posts about versioning assemblies in .NET Core. I have .NET Framework assemblies that use the auto-versioning where, in assemblyInfo.cs, I set the [assembly: AssemblyVersion("2.1.*")] attribute. When I build the assembly I can check it's properties and the file and product versions both reflect a version like 2.1.6985.26662, where 6985 and 26662 are the automatically populated build and revision numbers.
I understand that in .NET Core I can set the assembly info in the project file, so I started with the following in my project file:
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
<UserSecretsId>7c702802-48ca-46ea-8a7a-1fa89100baef</UserSecretsId>
<Deterministic>false</Deterministic>
<AssemblyVersion>2.1.*</AssemblyVersion>
</PropertyGroup>
The Deterministic=false is necessary in order for the version to contain a wildcard. Elsewhere in my code I access and expose the version, accessing it with
Version serviceVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
DateTime buildDate = new DateTime(2000, 1, 1).AddDays(serviceVersion.Build).AddSeconds(serviceVersion.Revision * 2);
string reportVersion = "{serviceVersion} ({buildDate})";
When I run that code, the build and revision are always 0. .NET Core accepts the wildcard in the version, but doesn't appear to automatically populate the build and revision numbers.
If I remove the assembly version from the project file and put it in an assemblyInfo.cs file instead, it seems to work as I expect it to. So I end up with this in my project file:
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
<UserSecretsId>7c702802-48ca-46ea-8a7a-1fa89100baef</UserSecretsId>
<Deterministic>false</Deterministic>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
The GenerateAssemblyInfo = false is necessary so that automatically generated assembly attributes don't conflict with my explicitly set attributes. And I've got this in my assemblyInfo.cs file:
[assembly: AssemblyVersion("2.1.*")]
The reflection code I use to read and access the version is the same, but it now has the expected build and revision numbers.
Does anyone know if the automatically set build and revision numbers are supposed to work when the assembly version is set with a wildcard in the project file? Or I am doing something wrong that causes that not to work?
Upvotes: 2
Views: 1056