首页 > 解决方案 > The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception

问题描述

I am working on a C# console application (.NET 4.6.1) and when running its executable I get the following error:

The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception.

Error description:

at LibGit2Sharp.Core.NativeMethods.git_repository_open(git_repository*& repository, FilePath path)
at LibGit2Sharp.Core.Proxy.git_repository_open(String path)
at LibGit2Sharp.Repository..ctor(String path, RepositoryOptions options, RepositoryRequiredParameter requiredParameter)
at LibGit2Sharp.Repository..ctor(String path)
at MyProj.MyClass.GetMaxBranchVersion(String repoPath)

Inner exception:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. File name: 'System.Runtime.InteropServices.RuntimeInformation, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' at LibGit2Sharp.Core.Platform.get_OperatingSystem() at LibGit2Sharp.Core.NativeMethods..cctor()

Now, the project's code works fine when debugging it or just running it with Visual Studio - the error comes up when trying to run it as an executable. The issue is not really with the GetMaxBranchVersion method itself, because I tried not calling it at all and this only caused the error to come up anyway, when running the next method.

I tried adding the following configuration items to the .csproj file as per this answer but unfortunately, it did not solve my problem:

<PropertyGroup>
..
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
..
</PropertyGroup>

What could be the possible reason?

I understand the information provided might not be a lot, but I can provide additional details if needed.

标签: c#gitvisual-studio-2017console-applicationlibgit2sharp

解决方案


我设法通过以下方式解决了我的问题:

  1. 将项目更改Target Framework.NET 4.7
  2. 复制了System.Runtime.InteropServices.RuntimeInformation.dll我试图从中运行可执行文件的文件夹内部

在这一点上,我得到了另一个错误:

System.DllNotFoundException:无法加载 DLL 'git2-6311e88':找不到指定的模块。(来自 HRESULT 的异常:0x8007007E)在 LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
在 LibGit2Sharp.Core.NativeMethods.LoadNativeLibrary() 在 LibGit2Sharp.Core.NativeMethods..cctor()

多亏了这个答案,我才能够通过复制git2-6311e88.dll(在我的情况下需要 x86 版本,在我的情况下通常位于您的文件夹bin/debugbin/release文件夹lib/os-versionlib/win32)来解决这个问题

瞧!有用!祝所有在同样问题上苦苦挣扎的人好运。


推荐阅读