0

I am running the Firestore emulator as a Docker container and facing two issues. Here's the code I'm using:


Environment.SetEnvironmentVariable("FIRESTORE_EMULATOR_HOST", "localhost:8080");

var builder = new FirestoreDbBuilder
{
    ProjectId = "default",
    EmulatorDetection = EmulatorDetection.EmulatorOrProduction
};
var firestoreDb = builder.Build();
    
var col = firestoreDb.Collection("Cities");
var docRef = col.Document("YVR");
var result = await docRef.SetAsync(new City { Name = "Vancouver", Population = 675_000 });
var doc = await docRef.GetSnapshotAsync();
doc.Exists.Dump();

var document = firestoreDb.Collection("Cities").Document("YVR");
var snapshot = await document.GetSnapshotAsync();
snapshot.Exists.Dump();
var city = snapshot.ConvertTo<City>();
city.Dump();

City is defined as:

[FirestoreData]
public class City
{
    [FirestoreDocumentId]
    public string Id { get; set; }

    [FirestoreProperty]
    public string Name { get; set; } = null!;

    [FirestoreProperty]
    public int Population { get; set; }
}

Question 1

Without specifying ProjectId, I get an exception ArgumentNullException, Value cannot be null. (Parameter 'projectId'). Shouldn't a null value resolve to the emulator since the FIRESTORE_EMULATOR_HOST environment variable is provided? enter image description here

And what project ID value should it be if working with the emulator?

Question 2

If I put a value for ProjectId, the code works, but using the emulator UI at the default URL http://127.0.0.1:4000/firestore/data doesn't show the document I'm adding.

8
  • Have you checked this thread1 for connecting to emulator using c# and thread2 Commented Dec 22, 2023 at 11:57
  • Yes, I've seen those two. Both are not applicable. In my case I can write and read, just don't see it in the emulator UI. Which, I suspect, has to do with the project ID. But GCP's documentation is so confusing I can't sort it out. Commented Dec 22, 2023 at 14:18
  • Can I check which emulator you're using? (All my personal experience is via gcloud emulators firestore start but that doesn't have a UI, so presumably you're using the one documented at firebase.google.com/docs/emulator-suite?) Commented Dec 22, 2023 at 16:20
  • That's the one, @JonSkeet. It comes as an NPM package, which we wrap in a Docker image. This is something I wish GCP would do themselves. And I think I've figured out what's the issue - it's very coupled to the ProjectId. So when building the image and running as a container, the project ID used in the code must match the one baked into the image. I will need to check if omitting the project ID from the image would allow having FirestoreDbBuilder created w/o ProjectID and let the emulator be used with different .NET projects. Hence why I wish the image would be provided by Google :) Commented Dec 22, 2023 at 21:17
  • @SeanFeldman: If you don't specify the ProjectId in the FirestoreDbBuilder, the library will (currently) try to determine it by contacting the GCP metadata server - that's probably not what you want in this case. We could potentially have a different defaulting approach when connecting to the emulator, but that's not something that's going to happen imminently. Commented Dec 22, 2023 at 21:32

1 Answer 1

0

I’m posting this answer as community wiki for better community visibility.

As it's very coupled to the ProjectId. when building the image and running as a container, the project ID used in the code must match the one baked into the image.

As per discussion on the github issue you raised, current workaround is to:

Using an environment variable in the Docker container (which can then be passed in when starting it). I assume the Docker image uses (or could use) the --project flag. If you take that from an environment variable, you could pass that in when starting the container, so you only need a single image which can work with any project. You'd still need to specify the same project ID in the .NET code, but at least you wouldn't need multiple images.

Sign up to request clarification or add additional context in comments.

3 Comments

Roopa M, you're rushing with the answer here. This is not the answer I would provide based on the findings in several repos. And, as a user of the NPM emulator, this is not the type of guidance that would help me.
@Sean Feldman I apologize for my action, can you provide your detailed solution as answer?
No need to apologize. Once GCP teams get back to me with specific information for a complete answer, I'll post it if it differs from yours. Otherwise, I'll accept yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.