Apple M1上.NET 6调用Firestore报libgrpc_csharp_ext.x64.dylib加载错误
问题根因
该错误由Google.Cloud.Firestore 2.4.0默认依赖的原生gRPC实现Grpc.Core导致,该库已停止维护,其原生二进制文件在macOS Big Sur + .NET 6 RC环境下存在签名、架构兼容类加载问题。
推荐解决方案(优先选择)
升级依赖并切换到托管gRPC实现,完全规避原生库兼容问题:
- 第一步:更新NuGet包
将Google.Cloud.Firestore升级到2.6.0及以上稳定版,同时安装Google.Api.Gax.Grpc.GrpcNetClient包。 - 第二步:添加gRPC配置
调整ConfigureFirestoreEmulator方法代码,显式指定使用托管gRPC实现,并开启本地gRPC非加密通道支持:static void ConfigureFirestoreEmulator(IConfigurationBuilder configurationBuilder) { // 开启.NET 6下非加密HTTP2 gRPC请求支持 AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); Environment.SetEnvironmentVariable("FIRESTORE_EMULATOR_HOST", "localhost:8080"); // 全局指定gRPC使用Grpc.Net.Client托管实现,不依赖原生库 Google.Api.Gax.Grpc.GrpcAdapter.Default = Google.Api.Gax.Grpc.GrpcNetClientAdapter.Default; var db = new FirestoreDbBuilder { ProjectId = "dev", EmulatorDetection = Google.Api.Gax.EmulatorDetection.EmulatorOnly }.Build(); }
备选解决方案(不升级Firestore版本时使用)
如果暂时无法升级Firestore包,可通过修复原生库签名/指定架构解决:
- 方案1:手动签名原生库
先安装Xcode命令行工具:
对报错路径的原生库执行自签名:xcode-select --install
完成后到系统「设置>安全与隐私>通用」中允许该库运行。codesign -f -s - /Users/[user]/Code/[project]/platform/WebApi/bin/Release/net6.0/runtimes/osx-x64/native/libgrpc_csharp_ext.x64.dylib - 方案2:指定运行架构
如果你使用的是M1系列Mac,在项目.csproj文件中添加运行时标识配置:
运行项目时指定架构:<PropertyGroup> <RuntimeIdentifier>osx-x64</RuntimeIdentifier> </PropertyGroup>dotnet run -r osx-x64
验证步骤
- 执行
firebase emulators:start确认Firestore模拟器正常启动,8080端口无占用 - 清理项目缓存后重新构建运行即可。
内容的提问来源于stack exchange,提问作者Frankely Diaz
相关产品推荐
相关产品推荐

