Windows平台C#程序突发无法连接Firestore问题求助
Windows端Firestore连接异常(无代码变更)排查方案
此前Windows(.NET Framework 4.7.2)、Android和iOS(.NET MAUI 7.0)端应用均可正常连接Firestore。昨日起无任何代码变更,但Windows端突然无法连接,抛出如下错误:
Exception thrown: 'Grpc.Core.RpcException' in mscorlib.dll Grpc.Core.RpcException: Status(StatusCode="Unavailable", Detail="failed to connect to all addresses", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1689930000.542000000","description":"Failed to pick subchannel","file":"..\\..\\..\\src\\core\\ext\\filters\\client_channel\\client_channel.cc","file_line":3218,"referenced_errors":[{"created":"@1689930000.542000000","description":"failed to connect to all addresses","file":"..\\..\\..\\src\\core\\lib\\transport\\error_utils.cc","file_line":165,"grpc_status":14}]}") at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Api.Gax.Grpc.ApiCallRetryExtensions.<>c__DisplayClass0_0`2.<<WithRetry>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Cloud.Firestore.WriteBatch.<CommitAsync>d__15.MoveNext() in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/WriteBatch.cs:line 226 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Cloud.Firestore.DocumentReference.<CreateAsync>d__19.MoveNext() in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/DocumentReference.cs:line 106 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Google.Cloud.Firestore.CollectionReference.<AddAsync>d__12.MoveNext() in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/CollectionReference.cs:line 94 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at WindowsEnviromentTestDigitalForm.Form1.<btnInsertFix_Click>d__3.MoveNext()
连接代码如下:
string path = AppDomain.CurrentDomain.BaseDirectory + @"privatykey.json"; Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path); FirestoreDb databaseFS = FirestoreDb.Create("appname"); CollectionReference collectionReference = databaseFS.Collection("Collection_Name"); Dictionary<string, object> map = new Dictionary<string, object>() { {"Test1", 0}, {"Test2", 1}, {"Test3", 3} }; collectionReference.AddAsync(map);
可能原因及解决方法
1. Windows系统网络环境变化
- 防火墙/杀毒软件拦截:Windows防火墙或第三方杀毒软件可能更新规则,拦截了Grpc通信端口(Firestore默认用443端口,但HTTP/2多路复用可能被误判)。
- 解决:将应用加入防火墙白名单,临时关闭杀毒软件测试;检查Windows Defender的应用控制规则。
- 代理配置变更:系统代理、VPN设置被修改,导致Grpc无法解析Firestore地址。
- 解决:检查代理设置,确保可正常访问
firestore.googleapis.com;关闭代理后测试连接。
- 解决:检查代理设置,确保可正常访问
2. Grpc依赖兼容性问题
- NuGet包版本冲突:.NET Framework 4.7.2下的
Grpc.Core、Google.Cloud.Firestore可能存在隐性版本变更(自动还原时更新),与MAUI端版本不一致。- 解决:锁定与MAUI端一致的NuGet包版本,清理缓存后重新安装依赖。
- HTTP/2支持缺陷:.NET Framework对HTTP/2的支持不完善,导致连接失败。
- 解决:强制Grpc使用HTTP/1.1,修改初始化代码:
var channel = new Grpc.Core.Channel("firestore.googleapis.com:443", Grpc.Core.ChannelCredentials.SecureSsl, new[] { new Grpc.Core.ChannelOption(Grpc.Core.ChannelOptions.HttpProtocolVersion, Grpc.Core.HttpProtocolVersion.Http1) }); var client = new Google.Cloud.Firestore.V1.Firestore.FirestoreClient(channel); FirestoreDb databaseFS = FirestoreDb.Create("appname", client);
- 解决:强制Grpc使用HTTP/1.1,修改初始化代码:
3. 服务账号密钥问题
- 文件权限丢失:Windows下应用运行用户失去
privatykey.json的读取权限(文件移至受保护目录或权限被修改)。- 解决:检查密钥文件的读取权限,将文件移至明确路径(如
C:\temp)后修改代码路径测试。
- 解决:检查密钥文件的读取权限,将文件移至明确路径(如
- 密钥失效:服务账号密钥在Google Cloud Console被撤销,Windows端缓存凭证失效(移动端缓存未过期)。
- 解决:登录Google Cloud Console检查密钥状态,重新生成密钥文件替换旧文件。
4. 临时网络或服务端问题
- 局部网络波动:Windows所在网络出现DNS解析失败、丢包,导致Grpc无法连接。
- 解决:更换网络(如手机热点)测试;运行
nslookup firestore.googleapis.com检查DNS解析,telnet firestore.googleapis.com 443测试端口连通性。
- 解决:更换网络(如手机热点)测试;运行
- Google服务局部故障:Windows端连接的Firestore节点临时故障。
- 解决:确认Google Cloud服务状态,等待一段时间后重试。
内容的提问来源于stack exchange,提问作者WhyAlfred
相关产品推荐
相关产品推荐

