libgit2sharp icon indicating copy to clipboard operation
libgit2sharp copied to clipboard

LibGit2Sharp: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. LibGit2Sharp: Unable to load DLL 'git2-15e1193': The specified module could not be found.

Open CalvinFengDatacom opened this issue 8 years ago • 31 comments

I'm using Azure Function App to create a app for updating Excel file to our ASP.Net Web Api project, which is hosting in Azure, and we are using Git to control the version of files.

Now, I'm having an issue of using LibGit2Sharp. I'd like to use Commands.Checkout() command to switch between branches. However, I got this issue: LibGit2Sharp: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. LibGit2Sharp: Unable to load DLL 'git2-15e1193': The specified module could not be found.

I'm using the latest version of LibGit2Sharp version 0.24.0, and I see it depends on LibGit2Sharp.NativeBinaries": "[1.0.185]"

Does anyone who has any idea how to solve this issue on Function App?

Thanks

CalvinFengDatacom avatar Feb 11 '18 22:02 CalvinFengDatacom

There might be some changes in the native dependencies in 2.1 docker images. The above method worked in 2.0 jessie images, but does not work on 2.1 because only stretch-based images are available.

jeremymeng avatar Jun 12 '18 20:06 jeremymeng

#1582 should hopefully be addressing this.

bording avatar Jun 12 '18 21:06 bording

I have the same issue when trying to run LibGit2Sharp in PowerShell Core on macOS:

> [LibGit2Sharp.Repository]::new($PWD)
Exception calling ".ctor" with "1" argument(s): "The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception."
At line:1 char:1
+ [LibGit2Sharp.Repository]::new($PWD)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TypeInitializationException

LibGit2Sharp v0.25.2

felixfbecker avatar Jun 18 '18 05:06 felixfbecker

@felixfbecker That doesn't sound like it has anything to do with this issue, which is regarding Azure Functions.

ethomson avatar Jun 18 '18 08:06 ethomson

I figured I would comment here since it's the same error message and people above also said they are having the same issue on Linux and in Linux Docker containers. Should I open a new issue?

felixfbecker avatar Jun 18 '18 16:06 felixfbecker

Yeah, this thread has become very messy with similar problems (can't load the native libraries) but very different root causes / platforms. A new issue would be helpful.

ethomson avatar Jun 18 '18 16:06 ethomson

@ethomson filed https://github.com/libgit2/libgit2sharp/issues/1583

felixfbecker avatar Jun 18 '18 20:06 felixfbecker

I have a similar issue building *.msi Windows Installer file. When I include following files to the output folder, it works. \lib\win32\x64\git2-1196807.dll and \lib\win32\x86\git2-1196807.dll

phuongph avatar Oct 22 '18 21:10 phuongph

I got similar error when running under target framework of 4.6.1. When I changed target framework to 4.6.2 the problem solved.

yury-kozlov avatar Feb 13 '19 15:02 yury-kozlov

Found temporary solution for docker:

export LD_LIBRARY_PATH=/root/.nuget/packages/gitversion.tool/5.1.2/tools/netcoreapp3.0/any/runtimes/debian.9-x64/native/

deadman2000 avatar Nov 30 '19 09:11 deadman2000

An update for those trying to get libgit2sharp to work in Azure Functions. (This is just my own experience. YMMV.)

If you publish your project with default "portable" (i.e. "any" platform), then libgit2sharp will use the the native libgit2 library from the linux-x64 runtimes folder. Unfortunately, that one is compiled against libssl 1.0 which is deprecated in favor of libssl 1.1.

On some Linux environments, libssl 1.0 is pre-installed. However, Linux-based Azure Functions currently uses Debian 9 ("stretch" ) or Debian 10 ("buster") as its runtime. Debian 9 has a libssl 1.0 library available, but not pre-installed. Debian 10 does not have a libssl 1.0 library at all.

Thankfully, libgit2sharp has a Debian 9 native libgit2 library in its runtimes folder, which was built against libssl 1.1. Therefore, all you need to do to work around this issue is publish using the Debian 9 runtime identifier, which is debian.9-x64. That will work with Linux-based Azure Functions on Linux, with or without containers.

If you publish on the command line, add -r debian.9-x64 to your dotnet publish command (in your dockerfile if you use containers).

If you publish from Visual Studio, first create a publishing profile (or edit your existing one), selecting linux-x64 from the target runtime dropdown. Then go edit the .pubxml file for that profile and change to <RuntimeIdentifier>debian.9-x64</RuntimeIdentifier>. (The two steps are needed only because the UI doesn't let you type in other RIDs.)

If you publish from VSCode, edit the .vscode/tasks.json file, find the publish task, and add "--runtime", "debian.9-x64", to the list of args.

mattjohnsonpint avatar Jul 20 '20 19:07 mattjohnsonpint