google-cloud-dotnet icon indicating copy to clipboard operation
google-cloud-dotnet copied to clipboard

Trouble Calling SnapshotAsync on a Collection Reference

Open ryanmats opened this issue 8 years ago • 5 comments

Here is the Program.cs file: https://gist.github.com/ryanmats/52535d2242881a58302e64c07f4e2dd3 Here is the QuickStartFirestore.csproj file: https://gist.github.com/ryanmats/8c1673a90de3d07db9c156f0352dd07b You can run the code by running dotnet run retrieve-all-documents YOUR_PROJECT_ID from command line.

Here is the error message I'm getting: https://gist.github.com/ryanmats/59cc0567b93317dc857c112b556a9349

One potential issue we noticed was that there was a client library change fairly recently that renamed the SnapshotAsync method to GetSnapshotAsync client library change (24 days ago)

However it looks like this change isn't yet propagated to the public packages on Nuget cause when Program.cs is updated to use GetSnapshotAsync the error returned is "'...'CollectionReference' does not contain a definition for 'GetSnapshotAsync'..." Error Message - https://gist.github.com/ryanmats/04a49f75b8806550175a8d8542d2d0fb

Thanks in advance for the help!

ryanmats avatar Mar 16 '18 20:03 ryanmats

Yes, if you're using 1.0.0-beta02, the name change is irrelevant.

Do you get that message every time, or just sometimes? Do you have any documents in that collection?

jskeet avatar Mar 16 '18 20:03 jskeet

The error message appears every time.

Yes, I have 2 documents in the users collection.

ryanmats avatar Mar 16 '18 20:03 ryanmats

Hmm. Slightly shorter program which works fine for me - could you try this?

Project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Google.Cloud.Firestore" Version="1.0.0-beta02" />
  </ItemGroup>
</Project>

Code:

using System;
using System.Threading.Tasks;
using Google.Cloud.Firestore;

class Program
{
    static async Task Main(string[] args)
    {
        FirestoreDb db = FirestoreDb.Create(args[0]);
        CollectionReference usersRef = db.Collection("users");
        QuerySnapshot snapshot = await usersRef.SnapshotAsync();
        foreach (DocumentSnapshot document in snapshot.Documents)
        {
            Console.WriteLine(document.Reference);
            foreach (var entry in document.ToDictionary())
            {
                Console.WriteLine($"{entry.Key} = {entry.Value}");
            }
        }
        Console.WriteLine("Done");
    }
}

I'll try your exact code now...

jskeet avatar Mar 16 '18 20:03 jskeet

Ah - I see what's wrong with your code - you're calling an async method, but not waiting for it to finish.

If you just change this:

RetrieveAllDocuments(project);

into

RetrieveAllDocuments(project).Wait();

... and change the async method to return Task instead of void, then it should work. (Or use an async Main method like my example, although that requires C# 7.1.)

Basically at the moment you've got an async method still running while the app is shutting down - I'm not sure there's anything we can do to make that work better.

jskeet avatar Mar 16 '18 20:03 jskeet

thanks @jskeet it is working now! Completely new to C# so this is helpful.

ryanmats avatar Mar 16 '18 22:03 ryanmats