How to Manually Set Assembly Version

Viewed 5159

This one is giving me such a headache. We used to put things in the project properties under [assembly: AssemblyVersion("1.0.0.0")] I am totally fine with change so I do not care where it is. I understand they are going to a new standard for versions as well, also totally fine.

Plenty of documents out there point to the project.json file which is clearly a waste as this is no longer a legit file. More recent say add the following to your .csproj file:

<PropertyGroup>
    <VersionPrefix>1.2.3</VersionPrefix>
    <VersionSuffix>alpha</VersionSuffix>
</PropertyGroup>

Also a total waste. Only because I can not seem to be able to read it. The following always gives me 1.0.0.0.

PlatformServices.Default.Application.ApplicationVersion

Not to mention when I right-click in File Explorer and click Properties, then Details tab also always says 1.0.0.0.

So, how I can set the version of each assembly within my solution AND then read them later at runtime?

2 Answers

Turns out this started working when .Net Core 2.0 came out. When you right-click on the Project and then click Properties there is a UI for it as seen below:

UI of Project Properties

The Package Version, Assembly Version and Assembly File Version correspond to the Version, AssemblyVersion and FileVersion settings in the .csproj file respectively:

<PropertyGroup>
  <Version>1.1.0</Version>
  <AssemblyVersion>1.1.0.9</AssemblyVersion>
  <FileVersion>1.1.0.9</FileVersion>
</PropertyGroup>

I then created a utility method in each project of my solution that does the following:

public static VersionInformationModel GetVersionInformation() {
  var Result = new VersionInformationModel {
    Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(),
    BuildDate = System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location),
    Configuration = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyConfigurationAttribute>().Configuration,
    TargetFramework = Assembly.GetExecutingAssembly().GetCustomAttribute<System.Runtime.Versioning.TargetFrameworkAttribute>().FrameworkName,
  };

  return Result;
}

So on an admin page of my site, I can know when version and other particulars about each project as it stands on whatever server I am looking at.

Related