Tanmay Bairagi
Tanmay Bairagi

Reputation: 624

LibGit2Sharp DllNotFoundException: Unable to load DLL 'git2-106a5f2'

I am working on a vsix project where I need to get information about a local git directory. I am following this article. When I am using LibGit2Sharp library, I am getting an exception as described in following image and error:-

How can I resolve this?

VS version details:

Visual Studio 2019

.Net Framework 4.7.2

Upvotes: 16

Views: 5771

Answers (5)

Dariusz Woźniak
Dariusz Woźniak

Reputation: 10350

LibGit2Sharp has a dependency on the git2-106a5f2.dll which is under [Debug|Release]\lib\win32\[x86|[x64] directory.

If that particular version is missing, you may need to reinstall LibGit2Sharp library, but uninstall LibGit2Sharp.NativeBinaries library between uninstall and install.

The following operations on NuGet packages should help:

  1. Uninstall LibGit2Sharp.
  2. Uninstall LibGit2Sharp.NativeBinaries.
  3. Install LibGit2Sharp.

Note: do not update LibGit2Sharp.NativeBinaries even if there is a newer version.

Upvotes: 3

Sajal
Sajal

Reputation: 55

For me, I manually installed the Debug and Release VSIX, and tested against regular VS instance and it worked fine. The LibGit2Sharp threw a DllNotFoundException only when debugging my code via the Exp version of Visual Studio 2019.

So I deleted the entire Extensions folder of the Exp verison of Visual Studio. It is the folder where the Exp version of VS installs all Plugins, like other versions of VS: %AppData%\Local\Microsoft\VisualStudio\16.0_a31c0a3aExp\Extensions

The next time I debugged my VSIX through Visual Studio, it worked. Hope this helps, I think it has something to do with extensions.en-US files inside that folder. Deleting just my plugin folder insted the Extensions folder did nothing.

Upvotes: 1

purnadika
purnadika

Reputation: 292

I got the same issue. It turn out I ignored the dependecies. Installing its dependency : LibGit2Sharp.NativeBinaries sort out my problem.

Upvotes: 1

Jonas Johansson
Jonas Johansson

Reputation: 341

I had the same issue and I solved it like this:

  1. Copy the dir folder from VisualStudioExtension -> Bin -> Debug -> lib to the root of the VisualStudioExtension project. This folder contains the DLL files required for LibGit2Sharp to work.
  2. Drag the lib folder onto the VisualStudioExtension project in Visual Studio.

enter image description here

  1. Select the dll files, right-click, Properties, and set it to Copy always and include in VSIX.

enter image description here

That worked for me.

Upvotes: 4

Edward Thomson
Edward Thomson

Reputation: 78703

LibGit2Sharp is a managed language (.NET) wrapper around a native (C) library, libgit2. It's a P/Invoke layer, plus a layer to give .NET semantics (objects, etc). This means, though, that you require both the LibGit2Sharp.dll (the managed-language side) and the git2-xxxxxxx.dll (the corresponding native library) that it calls into.

The native DLL is part of the LibGit2Sharp.NativeBinaries project that LibGit2Sharp takes a dependency on. It should be installed (transitively) when you install LibGit2Sharp itself. And although it tries to set itself up as a dependency that will be installed in the output directory, since native binaries are not well-understood in the .NET project world, this can sometimes fail, especially for more complex project types like VSIX.

Ultimately, LibGit2Sharp will look for the corresponding native DLL alongside where it's located. So within your output directory, wherever your VSIX is being executed from, try copying the git2-xxxxxxx.dll that is part of the LibGit2Sharp.NativeBinaries project to that location.

Once you've identified the correct location for the git2-xxxxxxx.dll binary to live, you should copy this as part of your installation for the project. (eg Build action: None, Copy to output directory: Copy always)

Upvotes: 8

Related Questions