Search code examples
c#libgit2sharp

The type initializer for ‘LibGit2Sharp.Core.NativeMethods’ threw an exception


Specific Exception received: “Could not load file or assembly ‘System.Runtime.InteropServices.RuntimeInformation, Version=0.0.0.0, Culture=neutral’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference.”

I have two projects, one is a sort of “Proof of concept” and the exact same code works perfectly there, while the other is a large solution in which I’m adding code to one of the existing projects.

I am getting an exception in the second (failing) project when running the following line of code:

LibGit2Sharp.Repository.Clone(baseGitHubURL + ourOrg + “/“ + ourRepo.Name, ourRepoPath + @“\” + ourRepo.Name, co);

Ahead of this code, I’ve got it printing the arguments being passed, which are as follows in the output from BOTH projects (again - identical code in each):

GitHub path: https://github.company.com/TESTING/CA4F

Filesystem path: C:\Users\User\CA4F

The creation of CloneOptions is identical in both projects with static values being used (it uses a personal access token as a username).

I am suspecting there is something wrong with how LibGit2Sharp was added to the failing project. Note that the PROCESS was the same for both:

From NuGet console: “Install-Package LibGit2Sharp”

Both have the same version (latest, version 0.25.0).

Despite these facts, the working project has a “Dependencies” section under the project listing LibGit2Sharp, while the non functioning project does not have this section. LibGit2Sharp WAS added as a reference to my actual project when I went to Nuget Package Manager and told it to install to ALL projects in the solution.

Another piece of information - the working project does NOT have a NuGet package for “LibGit2Sharp.NativeBinaries”, while the failing project does. On a whim, I tried deleting it from the failing project, but Nuget says “Unable to uninstall ‘LibGit2Sharp.NativeBinaries.1.0.210’ because ‘LibGit2Sharp.0.25.0’ depends on it.”


Solution

  • Just to close this up - I was able to resolve this by adding the following to the first configuration property group under the PropertyGroup tag in the csproj of every single project in the solution:

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

    After doing that, a recompile and run allowed me to successfully call LibGit2Sharp! :)

    Note that this was the solution provided by “bording” over on the LibGit2Sharp git issues “forum”.