c# - 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.
解决方案
我设法通过以下方式解决了我的问题:
- 将项目更改
Target Framework
为.NET 4.7
- 复制了
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/debug
或bin/release
文件夹lib/os-version
下lib/win32
)来解决这个问题
瞧!有用!祝所有在同样问题上苦苦挣扎的人好运。
推荐阅读
- java - 当我们转换为 Map 时出现 Json 解析异常
- mysql - 如果我抛出错误,是否需要释放连接?
- qt - 从文件系统加载图像在 Qt 样式表中不起作用
- android - 有什么方法可以检测到哪些应用程序我可以/不能访问他们的“应用程序信息”屏幕?
- python - Numpy 连接占用太多内存。有没有更有效的方法?
- c# - 自定义控件:在自定义控件中添加自定义按钮点击逻辑
- python-3.x - VS 代码 pylint 错误和格式不起作用
- javascript - 静音和取消静音按钮
- windows - 如何为指向特定 firebird 数据库存档的应用程序创建快捷方式?
- django-rest-framework - ListCreateAPIView Django 休息框架方法 \"POST\" 不允许