🐞 GitVersioning: 'ThisAssembly' Is Inaccessible Due to Its Protection Level

Posted on
pest-control csharp .NET nuget
thumbnail

😨 Problem

After I installed the Nerdbank.GitVersioning Nuget package in my .NET MVC app, the following error came out when I want to get the version using ThisAssembly.AssemblyInformationalVersion:

error CS0122: 'ThisAssembly' is inaccessible due to its protection level

I tried to install the package across all the projects in the same solution. It didn’t work.

I tried to uninstall and re-ininstall the package. It didn’t work.

πŸ˜€ Solution

.csproj

Turns out there is something missing in my .csproj file. I missed an <Import> tag at the end of the file, just before the </Target> tag:

<Import Project="..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets')" />

And in the EnsureNuGetPackageBuildImports target, add the following line:

<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets'))" />

In addition, the 1st <PropertyGroup> should contain a pair of NuGetPackageImportStamp tag:

<NuGetPackageImportStamp>
</NuGetPackageImportStamp>

AssemblyInfo.cs

I also removed the following lines in Properties/AssemblyInfo.cs:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

version.json

At the root directory of the project, I added a version.json file with the following content:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
  "version": "1.0.0"
}

After that, ThisAssembly is back and I can read the git version info successfully.

πŸ”— references:
πŸ–Ό cover image: Grace Hopper’s operational logbook for the Harvard Mark II computer