Home > Software design >  C# Creation of DB on Firestore fails with no error message
C# Creation of DB on Firestore fails with no error message

Time:08-16

I am following the docs on how to set up a backend with firestore:

https://firebase.google.com/docs/firestore/quickstart?authuser=0#c_1

I already set up a service account, genereted a key file and fed that into the code on my end. The connection works and I set the right permission (owner) to be able to read the bucket list.

But as soon as I try the first line of code from the tutorial:

FirestoreDb db = FirestoreDb.Create(project);
Console.WriteLine("Created Cloud Firestore client with project ID: {0}", project);

The execution dies. It doesnt lead to an error message, it doesnt run into a catch block with an exception. It just doesnt continue after the Create(project) part.

I noticed however, that the created datebase on the firebase console and the service account dont seem to be connected yet. Also, I dont know what to put for "project". I tried the project Id from the service account (with which i can do listbuckets) but this doesnt seem to work.

In the docs it does not state anything about what else to do.

Can you guys give me a hint maybe?

Thank you

EDIT:

LONGER CODE EXCEPT:

 var credential = GoogleCredential.FromFile("/Users/juliustolksdorf/Projects/Interior Circle/keys/interiorcircle-4f70f209e160.json");
                var storage = StorageClient.Create(credential);

                // Make an authenticated API request.
                var buckets = storage.ListBuckets("interiorcircle");
                foreach (var bucket in buckets)
                {
                    Console.WriteLine(bucket.Name);
                }


                var db = FirestoreDb.Create("interiorcircle");
                DocumentReference docRef = db.Collection("users").Document("alovelace");
                Dictionary<string, object> user = new Dictionary<string, object>
                {
                     { "First", "Ada" },
                     { "Last", "Lovelace" },
                     { "Born", 1815 }
                };
                await docRef.SetAsync(user);


            }
            catch(Exception e)
            {
                DisplayAlert("hi", e.ToString(), "ok");

            }

List buckets works, so the key is set correctly, but as soon as I try to do the create DB it fails.

CodePudding user response:

You should refer to the Firestore .NET Client Documentation.

In order to connect, you should pass the projectId to FirestoreDb.Create and you should set and environment variable called GOOGLE_APPLICATION_CREDENTIALS which contains the path to your service account JSON file.

Edit:

You can also explicitly pass the credential to FirestoreDb, by using:

FirestoreDb db = new FirestoreDbBuilder { ProjectId = projectId, Credential = credential }.Build();
  • Related