How to connect a C# WinForms Application to Google Datastore Emulator running on a local Docker Instance

46 Views Asked by At

I have a requirement to connect a C# win forms application running inside of Visual Studio on my local machine, to an instance of Google Datastore Emulator running on a Docker container which is also running on my local machine.

I am able to connect to our hosted live and development datastores okay, and I can also connect from a different Docker container running our frontend NodeJS solution, but I just can't get C# to talk to Docker. Can anyone please shed any light on what I might be doing wrong?

This is the log output on Docker when I run dev_appserver.py:

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.

You can see that it reports that it starts the Cloud Datastore emulator at: http://localhost:40000

And this is the C# code I am attempting to connect with:

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}");
}

I have searched extensively for a solution, and neither ChatGPT or Bard have been of any help.

I've tried different ports, IPs (e.g. 0.0.0.0 and localhost).

If I uncomment the .Endpoint instruction for the DatastoreDbBuilder I get an error:

  • "InvalidOperationException : Endpoint is set, contrary to use of EmulatorDetection.EmulatorOnly ( Source=Google.Api.Gax )"

When I run the code as it stands I get an error on the db.Query line after about 30 seconds, stating:

  • Status(StatusCode="Unavailable", Detail="failed to connect to all addresses", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {etc...}"

Any help would be greatly appreciated. Thanks :)

0

There are 0 best solutions below