Mike Marshall
Mike Marshall

Reputation: 7850

Can't connect Firebase Cloud Firestore from C# .NET Core app

I'm trying to set up a very simple test application for connecting to my Firebase Cloud Firestore instance.

It's a C# .NET Core console application using .NET Core 3.0 on Windows 7

I'm just trying to connecting to my Firestore instance using my project name.

My GOOGLE_APPLICATION_CREDENTIALS environment variable is set to C:\test\creds.json which I created from using the "Create Key" feature on the GCP Console page for my default service account.

When I run the code below, I get the following error:

using Google.Cloud.Firestore;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            FirestoreDb db = FirestoreDb.Create("my-proj-id");
        }
    }
}

Exception:

The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.'

When I run ProcessMonitor to detect file system activity, my application never even tries to touch the creds.json file, which makes me believe the C# Cloud Firestore API isn't even finding my creds file. I've set the GOOGLE_APPLICATION_CREDENTIALS env variable at both the user and system level.

Is there a problem with how I've configured things?

Upvotes: 1

Views: 3375

Answers (3)

you need something like that

enter code here

    private void Conexion()  {
     string path = AppDomain.CurrentDomain.BaseDirectory + @"your.json";
     Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
     database = FirestoreDb.Create("yourproject-1f8dd");  }


async void Get_Document() {
    Conexion();

    //consulta de desviaciones en parametro de fechas
    Query Qref = database.Collection("test")
        .WhereGreaterThanOrEqualTo("fecha", desde)
    .WhereLessThanOrEqualTo("fecha", hasta);
    QuerySnapshot snap = await Qref.GetSnapshotAsync();

    DataTable dt = new DataTable();
    dt.Columns.Add("1");
    dt.Columns.Add("2");
    dt.Columns.Add("3");
    dt.Columns.Add("4");

    //dibuja el datagrid
    foreach (DocumentSnapshot docsnap in snap)
    {
        FirestoreData desv = docsnap.ConvertTo<FirestoreData>();

        if (docsnap.Exists)
        {
            dt.Rows.Add(desv.1
                , desv.2
                , desv.3
                , desv.4);
            dataGridView1.DataSource = dt;
        }
    } }

Upvotes: 1

Mario
Mario

Reputation: 426

Sometimes the environment that Visual Studio is running can be different to the scope of the PATH ENV variables in Windows. I think it is related to the Admin permission. It would be interesting to execute the task and the GOOGLE_APPLICATION_CREDENTIALS assignment opening Visual Studio as Administrator.

Seems that this behavior is expected. C# .net MVC, set path to Google Application Credentials JSON file

Upvotes: 0

Mike Marshall
Mike Marshall

Reputation: 7850

Well this was maddening. I tried to log the GOOGLE_APPLICATION_CREDENTIALS environment variable value in my app and it was null. I restarted Visual Studio and it started to work. My theories are that either VS passed the current environment to the debugee when it launches the debugger process or that it may use one of those Visual Studio debugger host proxy processes for debugging and it was restarted after I restarted the IDE

Upvotes: 1

Related Questions