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 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
Good question - LibGit2Sharp relies on libgit2 itself, a native library, which it accesses through PInvoke. I'm not familiar enough to know if you can use pinvoke with Azure Functions, or if we need to adjust the way we load the native libraries so that Azure Functions is successful.
@ethomson Thanks your for your reply, seems this is a quite new question of LibGit2Sharp. Can you tell me which PInvoke should I use? Can I find it through NuGet? If it's available on NuGet, then I can try to add it to my function app.
@CalvinFengDatacom You don't have to add anything. The LibGit2Sharp package includes a reference to the LibGit2Sharp.NativeBinaries package, which includes the required native library.
There must be something specific to how Azure Functions works that is preventing the native library from being in the expected location when your code runs.
@bording Thanks, I see when I reload the packages of my Azure function App, LibGit2Sharp.NativeBinaries/1.0.185 has been added automatically. But, I still 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. Maybe @ethomson is right, seems the way of loading native libraries to Azure Functions need to be adjusted.
But, I'm happy to try other ways if you guys have some idea. I can just setup a small Function App to try this.
Are you able to run this locally in Azure Functions Core Tools?
Are you set up for a 64 bit Azure Function runtime? https://blogs.msdn.microsoft.com/benjaminperkins/2017/02/09/azure-functions-on-64-bit/ Is there any effect from changing these settings?
@ethomson I have changed for using 64 bit of Azure function runtime, but still got the same result. I haven't try running it locally, should be the same I think.
@ethomson I have tried to debug Azure function locally from Visual studio by Attach Debugger. https://blogs.msdn.microsoft.com/webdev/2016/12/01/visual-studio-tools-for-azure-functions/
I got the same issue from log console: 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.
Um... Seems I have tried everything already.
@ethomson @bording Hi guys, any updates of using LibGit2Sharp in Azure function app? I have tried to run azure function app locally following this way https://blogs.msdn.microsoft.com/webdev/2016/12/01/visual-studio-tools-for-azure-functions/
But, I still got the same issue from log console.
I get the same message when trying to run a .NET Core app (v0.25.0-preview0073) inside a Linux docker container (using Windows 10 Docker for Windows).
According to this comment, I expected it works inside a Linux containers.
Running an app inside Windows container works ok, but it's not an option for me, because I already use Linux containers for another app (WIndows for Docker can not run both types of containers simultaneously).
@MikhailTymchukDX please try using a debian based container. If you're using alpine or centos, you will need to provide your own libgit2 binaries.
@ethomson Thanks for that tip!
I changed base image to microsoft/dotnet:2.0-sdk-jessie and everything works now on Linux container.
Changing to jessie didn't quite do the trick: I'm still getting the same error with this simple program
using LibGit2Sharp;
namespace TestClone
{
class Program
{
static void Main(string[] args) => Repository.Clone("https://github.com/tgstation/tgstation", "tgstation");
}
}
Only dep is LibGit2Sharp 0.25.0-preview-0081 Dockerfile:
FROM microsoft/aspnetcore:2.0.6-jessie AS base
WORKDIR /app
FROM microsoft/aspnetcore-build:2.0.6-2.1.101-jessie AS build
WORKDIR /src
COPY . .
RUN dotnet restore TestClone/TestClone.csproj
WORKDIR /src/TestClone
RUN dotnet publish -c Docker -o /app
FROM base AS final
WORKDIR /app
COPY --from=build /app .
RUN ls
RUN dotnet TestClone.dll
ENTRYPOINT ["./TestClone"]
Result:
1>Step 12/14 : RUN ls
1> ---> Running in 9142e6549a2a
1>LibGit2Sharp.dll
1>TestClone.deps.json
1>TestClone.dll
1>TestClone.pdb
1>TestClone.runtimeconfig.json
1>runtimes
1>Removing intermediate container 9142e6549a2a
1> ---> 0fb110df7e42
1>Step 13/14 : RUN dotnet TestClone.dll
1> ---> Running in d71b2bd4a62e
1>[91m
1>Unhandled Exception: [0m[91mSystem.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'git2-2d2a602': The specified module or one of its dependencies could not be found.
1> (Exception from HRESULT: 0x8007007E)
1> at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
1> at LibGit2Sharp.Core.NativeMethods.LoadNativeLibrary()
1> at LibGit2Sharp.Core.NativeMethods..cctor()
1> --- End of inner exception stack trace ---
1> at LibGit2Sharp.Core.NativeMethods.git_clone(git_repository*& repo, String origin_url, FilePath workdir_path, GitCloneOptions& opts)
1> at LibGit2Sharp.Core.Proxy.git_clone(String url, String workdir, GitCloneOptions& opts)
1> at LibGit2Sharp.Repository.Clone(String sourceUrl, String workdirPath, CloneOptions options)
1> at TestClone.Program.Main(String[] args) in /src/TestClone/Program.cs:line 9
1>[0m[91mAborted
1>Service 'mapdiffbot' failed to build: The command '/bin/sh -c dotnet TestClone.dll' returned a non-zero code: 134
EDIT: Upon further investigation this seems to be unique to the aspnetcore image. dotnet-sdk and aspnetcore-build work fine
I'm getting the same problem also (in a docker container). Maybe this is naive, but is the issue not just that the libgit2sharp.nativebinaries package ships the file as git2-version.dll on Windows, and libgit2-version.so on Linux?
There's some discussion of these naming issues in dotnet/coreclr#930.
(Using -jessie or -stretch didn't work for me.)
I actually got the aspnetcore image working by installing these packages: https://github.com/Cyberboss/MapDiffBot/blob/bcde6d6c150cafede52b419f61ead05896b316ab/MapDiffBot/Dockerfile#L22
On Thu, Mar 29, 2018, 8:59 PM Matt Johnson [email protected] wrote:
I'm getting the same problem also (in a docker container). Maybe this is naive, but is the issue not just that the libgit2sharp.nativebinaries package ships the file as git2-version.dll on Windows, and libgit2-version.so on Linux?
There's some discussion of these naming issues in dotnet/coreclr#930 https://github.com/dotnet/coreclr/issues/930.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/libgit2/libgit2sharp/issues/1533#issuecomment-377415868, or mute the thread https://github.com/notifications/unsubscribe-auth/AHyweofWO6Tv9daDDEZEl9sAymNukD0rks5tjYOEgaJpZM4SBidm .
I'm getting the same problem also (in a docker container). Maybe this is naive, but is the issue not just that the libgit2sharp.nativebinaries package ships the file as git2-version.dll on Windows, and libgit2-version.so on Linux?
@mj1856 That pattern is the one that is already accounted for via DllImport, so no that's not the issue.
There are two things that could be causing the problem:
- The native binary isn't being included in the container in the correct location for it to be found
- The base container image you're using doesn't have all of the native dependencies that libgit2 needs.
Ok, I got it working with Jessie. I'm not using asp.net core, just the dotnet base images. microsoft/dotnet:2.0-runtime-jessie and microsoft/dotnet:2.0-sdk-jessie. Of the dependencies listed in @Cyberboss's link, the only one I needed to install was libcurl3-gnutls. I'm guessing this is because I was cloning an https repo.
The -stretch images did not work, so I'm wondering what else needs to be installed there. Also, I'd prefer to use openssl instead of gnutls, but I'm not sure how to do that. Anyone know?
Thanks.
The method I went through to find what was missing was installing ld on
the image and then running it on libgit2-xxxxxxx.so to see what couldn't be
found.
On Apr 2, 2018 1:28 PM, "Matt Johnson" [email protected] wrote:
Ok, I got it working with Jessie. I'm not using asp.net core, just the dotnet base images. microsoft/dotnet:2.0-runtime-jessie and microsoft/dotnet:2.0-sdk-jessie. Of the dependencies listed in @Cyberboss https://github.com/Cyberboss's link, the only one I needed to install was libcurl3-gnutls. I'm guessing this is because I was cloning an https repo.
The -stretch images did not work, so I'm wondering what else needs to be installed there. Also, I'd prefer to use openssl instead of gnutls, but I'm not sure how to do that. Anyone know?
Thanks.
— You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/libgit2/libgit2sharp/issues/1533#issuecomment-377986020, or mute the thread https://github.com/notifications/unsubscribe-auth/AHywesxYAtIzolXpIVowkpWt-cxsY6LMks5tkl_HgaJpZM4SBidm .
did anyone managed to get this working inside an azure function?
I was having this same issue on a microsoft/aspnetcore:2.0 Docker image. I was getting this error: DllNotFoundException: Unable to load DLL 'git2-6311e88': So I followed the link from Cyberboss Then changed my image version from 2.0 to 2.0.7-jessie
FROM microsoft/aspnetcore:2.0.7-jessie AS base
Then I pasted this code right below the FROM line...
# Dependencies for libgit2
RUN apt-get update && apt-get install -y --no-install-recommends \
libc6 \
zlib1g-dev \
libcomerr2 \
libc6-dev \
libgcrypt20 \
libkeyutils1 \
libcurl3-gnutls \
libsasl2-2 \
libgpg-error0 \
&& rm -rf /var/lib/apt/lists/*
Then it worked. Then I rejoiced. Thanks Cyberboss!
I don't yet have a full understanding of Azure Functions, but: https://github.com/libgit2/libgit2sharp/issues/1574#issuecomment-389766476 indicates that this is indeed possible:
This morning I solved the problem. I used an Azure Function v1 (.NET Framework instead of .NET Standard) with the LibGit2Sharp 0.21.0.176. When you include the native binary in the release it is working!