DEV Community

Cover image for 🐞 GitVersioning: 'ThisAssembly' Is Inaccessible Due to Its Protection Level
Bemn
Bemn

Posted on • Originally published at bemnlam.github.io on

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

😨 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
Enter fullscreen mode Exit fullscreen mode

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')" />
Enter fullscreen mode Exit fullscreen mode

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'))" />
Enter fullscreen mode Exit fullscreen mode

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

<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
Enter fullscreen mode Exit fullscreen mode

AssemblyInfo.cs

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

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)