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

如何将C# WinForms应用连接到本地Docker中的Google Datastore Emulator

本地C# WinForms应用连接Docker中Google Datastore Emulator的问题

本地Visual Studio运行的C# WinForms应用,无法连接本地Docker容器内的Google Datastore Emulator实例。目前可正常连接托管的生产、开发环境数据存储,也能通过另一个运行前端NodeJS解决方案的Docker容器实现连接,但C#应用始终无法与Docker通信。

Docker容器运行日志

INFO     2023-11-21 23:40:06,300 devappserver2.py:240] Using Cloud Datastore Emulator.
INFO     2023-11-21 23:40:06,323 devappserver2.py:321] Skipping SDK update check.
INFO     2023-11-21 23:40:06,372 datastore_emulator.py:156] Starting Cloud Datastore emulator at: http://localhost:40000
INFO     2023-11-21 23:40:15,468 datastore_emulator.py:162] Cloud Datastore emulator responded after 9.096579 seconds
INFO     2023-11-21 23:40:15,470 <string>:400] Starting API server at: http://localhost:9000
INFO     2023-11-21 23:40:15,473 <string>:390] Starting gRPC API server at: http://localhost:40829
INFO     2023-11-21 23:40:18,289 dispatcher.py:276] Starting module "bend" running at: http://0.0.0.0:8080
INFO     2023-11-21 23:40:18,290 admin_server.py:70] Starting admin server at: http://0.0.0.0:8000
WARNING  2023-11-21 23:40:18,290 devappserver2.py:417] No default module found. Ignoring.

日志显示Cloud Datastore Emulator启动地址为:http://localhost:40000

尝试的C#连接代码

Environment.SetEnvironmentVariable("DATASTORE_EMULATOR_HOST", $"localhost:40000");
Environment.SetEnvironmentVariable("DATASTORE_HOST", $"http://localhost:40000");
Environment.SetEnvironmentVariable("DATASTORE_PROJECT_ID", "my-project-id");
Environment.SetEnvironmentVariable("FIRESTORE_EMULATOR_HOST", $"localhost:40000");
Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "DEBUG"); // enable traces

// Create a Datastore client
var db = new DatastoreDbBuilder
{
    ProjectId = "my-project-id",
    EmulatorDetection = EmulatorDetection.EmulatorOnly,
    //Endpoint = "http://localhost:40000",
}.Build();

// Now you can use 'db' to interact with the Datastore Emulator
var query = new Query("__kind__");
var results = db.RunQuery(query);

foreach (var entity in results.Entities)
{
    var kindName = entity.Key.Path[0].Name;
    Console.WriteLine($"Kind: {kindName}");
}

已尝试的操作及错误信息

  • 尝试过不同端口和IP地址(如0.0.0.0、localhost)
  • 取消注释DatastoreDbBuilder的.Endpoint指令时,触发错误:
    InvalidOperationException : Endpoint is set, contrary to use of EmulatorDetection.EmulatorOnly ( Source=Google.Api.Gax )
    
  • 按当前代码运行时,约30秒后在db.RunQuery(query)行触发错误:
    Status(StatusCode="Unavailable", Detail="failed to connect to all addresses", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {etc...}"
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 18:20:29