You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows构建的ASP.NET Core应用在AlmaLinux 8上运行LibGit2Sharp遇加载问题

问题描述

ASP.NET Core 2.1应用在Windows环境构建,使用LibGit2Sharp 0.26.2版本,Windows本地运行正常,但部署到AlmaLinux 8服务器后无法工作。已将linux-x64原生运行时发布输出的libgit2-106a5f2.so文件复制到AlmaLinux机器上LibGit2Sharp.dll所在的同一目录,仍出现以下错误:

System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'git2-106a5f2' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libgit2-106a5f2: cannot open shared object file: No such file or directory
   at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
   at LibGit2Sharp.Core.NativeMethods.InitializeNativeLibrary()
   at LibGit2Sharp.Core.NativeMethods..cctor()
   --- End of inner exception stack trace ---
   at LibGit2Sharp.Core.NativeMethods.git_repository_open_ext(git_repository*& repository, FilePath path, RepositoryOpenFlags flags, FilePath ceilingDirs)
   at LibGit2Sharp.Core.Proxy.git_repository_open_ext(String path, RepositoryOpenFlags flags, String ceilingDirs)
   at LibGit2Sharp.Repository.IsValid(String path)
...

补充调试信息

设置LD_DEBUG和LD_LIBRARY_PATH环境变量后,加载libgit2库时得到如下输出:

1413948:     file=libgit2-106a5f2 [0];  dynamically loaded by /usr/share/dotnet/shared/Microsoft.NETCore.App/2.1.30/libcoreclr.so [0]
   1413948:     find library=libgit2-106a5f2 [0]; searching
   1413948:      search path=/path/to/application/runtimes/linux-x64/native                (LD_LIBRARY_PATH)
   1413948:       trying file=/path/to/application/runtimes/linux-x64/native/libgit2-106a5f2
   1413948:      search cache=/etc/ld.so.cache
   1413948:      search path=/lib64/tls:/lib64:/usr/lib64/tls:/usr/lib64                (system search path)
   1413948:       trying file=/lib64/tls/libgit2-106a5f2
   1413948:       trying file=/lib64/libgit2-106a5f2
   1413948:       trying file=/usr/lib64/tls/libgit2-106a5f2
   1413948:       trying file=/usr/lib64/libgit2-106a5f2

实际存在文件:/path/to/application/runtimes/linux-x64/native/libgit2-106a5f2.so,但系统查找的是不带.so后缀的文件。

解决方案
  • 创建软链接匹配查找名称:在库所在目录创建软链接,让系统能找到对应文件:
    ln -s /path/to/application/runtimes/linux-x64/native/libgit2-106a5f2.so /path/to/application/runtimes/linux-x64/native/libgit2-106a5f2
    
  • 检查并补全依赖库:如果创建软链接后仍报错,用ldd命令检查库的依赖是否齐全:
    ldd /path/to/application/runtimes/linux-x64/native/libgit2-106a5f2.so
    
    若输出中有not found的依赖,用dnf安装对应的包,例如:
    dnf install openssl-devel zlib-devel
    
  • 使用正确的发布方式:建议直接在Linux环境发布,或在Windows上用dotnet publish指定目标运行时,让SDK自动处理原生库部署:
    dotnet publish -c Release -r linux-x64 --self-contained false
    
  • 验证文件权限:确保应用进程对库文件和所在目录有读取权限:
    chmod 644 /path/to/application/runtimes/linux-x64/native/libgit2-106a5f2.so
    chmod 755 /path/to/application/runtimes/linux-x64/native/
    

内容的提问来源于stack exchange,提问作者jceddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 20:50:29