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

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay