MicrosoftLearning / dp-420-cosmos-db-dev

DP-420: Designing and Implementing Cloud-Native Applications Using Microsoft Azure Cosmos DB
https://microsoftlearning.github.io/dp-420-cosmos-db-dev/
MIT License
81 stars 95 forks source link

Unable to Execute Query #18

Closed tyrone0501 closed 9 months ago

tyrone0501 commented 2 years ago

Module: 05

Lab/Demo: 09

Task: 04

Step: 14

Description of issue Every time I try to run the query the following error appears:

handled exception. System.MissingMethodException: Method not found: 'System.String Microsoft.Azure.Documents.IAuthorizationTokenProvider.GetUserAuthorizationToken(System.String, System.String, System.String, Microsoft.Azure.Documents.Collections.INameValueCollection, Microsoft.Azure.Documents.AuthorizationTokenType, System.String ByRef)'.

Repro steps:

Included the following code with the correct endpoints and keys:

using System; using Azure.Cosmos;

string endpoint = "";

string key = "";

CosmosClient client = new CosmosClient(endpoint, key);

CosmosDatabase database = await client.CreateDatabaseIfNotExistsAsync("cosmicworks");

CosmosContainer container = await database.CreateContainerIfNotExistsAsync("products", "/categoryId");

string sql = "SELECT * FROM products p"; QueryDefinition query = new (sql);

await foreach (Product product in container.GetItemQueryIterator(query)) { Console.WriteLine($"[{product.id}]\t{product.name,35}\t{product.price,15:C}"); }

and received an error when I tried to run the application with donet run

MScalopez commented 2 years ago

Reviewing issue

safersephy commented 2 years ago

Problem seems to be related specifically to the Azure.Cosmos package and the CosmosClient. The Microsoft.Azure.Cosmos CosmosClient works without any issues, but lacks the ability to work with Asynchronous streams.

safersephy commented 2 years ago

It's probably best to update the used package to Microsoft.Azure.Cosmos and adapt the new pattern to iterate.

Replace:

await foreach (Product product in container.GetItemQueryIterator<Product>(query)) { Console.WriteLine($"[{product.id}]\t{product.name,35}\t{product.price,15:C}"); }

With:

`using FeedIterator feed = container.GetItemQueryIterator( queryDefinition: query );

while (feed.HasMoreResults) { FeedResponse response = await feed.ReadNextAsync(); foreach (Product item in response) { Console.WriteLine($"[{item.id}]\t{item.name,35}\t{item.price,15:C}"); } }`

I'd contribute myself but sadly i'm not a MCT :(

ejneuman commented 2 years ago

When you first open script.cs in Lab 09, you need to run the following terminal commands to make sure the code compiles correctly with the newer version of the framework.

  1. dotnet remove package Microsoft.Azure.Cosmos
  2. dotnet add package Azure.Cosmos --version 4.0.0-preview3
  3. dotnet build

You can then proceed as directed.

MScalopez commented 9 months ago

issue has been fixed