首页 > 解决方案 > C# 程序将数据保存到 Google FirestoreDB 在某些计算机上失败(StatusCode=Unavailable; grpc_status: 14)

问题描述

我有一个 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", MessageBoxButton.OK, MessageBoxImage.Error);            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType().ToString());
                MessageBox.Show("Error:\n\n" + ex.Message, "Error", MessageBoxButton.OK, 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 调用在某些计算机上不起作用?不知道我还能检查/尝试什么。谢谢!

标签: c#google-cloud-platformgrpc

解决方案


事实证明,问题在于出现问题的计算机中的时区不正确。

如果错误消息更具描述性,这将更容易识别,因为这些计算机确实与 Google 的服务器有网络连接。


推荐阅读