问题描述
我有一个 C# 程序,可以读取计算机规格并将它们保存到 Google FirestoreDB。在 95% 的计算机中,一切正常。但是有些计算机(找不到公分母)返回错误:
Grpc.Core.RpcException: Status(StatusCode=Unavailable,Detail="Failed to connect to all addresses")
grpc_status: 14
环境变量 GOOGLE_APPLICATION_CREDENTIALS 有正确的路径到包含此内容的 keyfile.json(从谷歌云控制台下载。敏感数据已删除):
{ "type": "service_account","project_id": "project-id","private_key_id": "XXXXXXXXXXXX","private_key": "-----BEGIN PRIVATE
KEY-----XXXXXXXXXXXX---END PRIVATE KEY-----\n","client_email":
"XXXXXX@XXXXXX.iam.gserviceaccount.com","client_id": "XXXXXX","auth_uri": "https://accounts.google.com/o/oauth2/auth","token_uri": "https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":
"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":
"https://www.googleapis.com/robot/v1/Metadata/x509/XXXXXX.iam.gserviceaccount.com"
}
这是将数据保存到 FirestoreDB 的 C# 代码:
try
{
string GoogleCloudProjectId = "project-id";
Environment.SetEnvironmentvariable("GOOGLE_APPLICATION_CREDENTIALS",AppDomain.CurrentDomain.BaseDirectory + "keyfile.json");
FirestoreDb db = FirestoreDb.Create(GoogleCloudProjectId);
DocumentReference docRef = db.Collection("inventory").Document(systemSpecs.ItemID);
await await docRef.SetAsync(systemSpecs).ContinueWith(q =>
{
if (q.IsCompleted && !q.IsFaulted)
{
MessageBox.Show("Added successfully to inventory.","Ok",MessageBoxButton.OK,MessageBoxImage.information);
}
return q;
});
}
catch (Grpc.Core.RpcException rpcEx) {
MessageBox.Show(rpcEx.Message,"Error",MessageBoxImage.Error); }
catch (Exception ex)
{
MessageBox.Show(ex.GetType().ToString());
MessageBox.Show("Error:\n\n" + ex.Message,MessageBoxImage.Error);
}
finally
{
Environment.SetEnvironmentvariable("GOOGLE_APPLICATION_CREDENTIALS",null);
Environment.Exit(0);
}
使用 NuGet (Visual Studio) 提供的最新包版本:
Google.Api.Gax = v3.2.0
Google Api.Gax.Grpc = v3.2.0
Google Api.Gax.Grpc.GrpcCore = v3.2.0
Google.Cloud.Firestore = v2.3.0
Google.Cloud.Firestore.V1 = v2.2.0
Grpc.Auth = v2.34.0
Grpc.Core = v2.34.0
Grpc.Core.Api = v2.34.0
计算机可以访问互联网(使用 wifi 和有线连接进行测试)。它 ping 很好 google.com、accounts.google.com、oauth2.googleapis.com、www.googleapis.com。 我还禁用了 Windows 防火墙,这没有任何区别。这种情况发生在运行 Windows 7 和 Windows 10 的计算机上。
使用相同的互联网连接,运行相同程序的其他计算机一切正常。
为什么 GRPC 调用在某些计算机上不起作用?不知道我还能检查/尝试什么。谢谢!
解决方法
事实证明,问题在于出现问题的计算机中的时区不正确。
如果错误消息更具描述性,这会更容易识别,因为计算机确实与 Google 的服务器建立了网络连接。