0

I am using Azure CosmosDB SQL API. I have an Asynchronous task to Connect to my AzureCosmosDB Account and I am trying to create a database through code.

Following is my code Snippet

private async Task ConnectToDocumentDB()
{
this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
Console.WriteLine(client.WriteEndpoint);
Database db = await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = "MyDB" });
Console.WriteLine("Response --" + db.Id);           
}

When I view the Client's EndPonit in console to ensure the AzureCosmosDb Connection, I am able to view the correct EndPoint. But when I try to create the Database, it doesn't create database.

The method call to this asynchronous task from main method is as follows

 Program p = new Program();
 p.ConnectToDocumentDB().Wait();

I dont know where I am going wrong.Help me with this issue.

Thanks in advance!

5
  • Did you try debugging through the code? What is the response from CreateDatabaseIfNotExistsAsync? The response is a ResourceResponse<Database> with an implicit operator so I'd rather you returned that instead so we can debug it using the response metadata. Commented Nov 27, 2018 at 10:05
  • @NickChapsas Yes!I tried debugging the code. But when the program control reaches the createDatabaseAsync Method call the program terminates and no exception is also thrown.So I am not able to view the ResourceResponse. Commented Nov 27, 2018 at 10:08
  • Did you try wrapping it in a try-catch and see if it is an exception that your exception settings are ignoring? Also don't use .Wait() on Program. Use .GetAwaiter().GetResult() instead. Commented Nov 27, 2018 at 10:10
  • @NickChapsas Thanks! It works fine now Commented Nov 27, 2018 at 11:33
  • I wrote up the answer. Feel free to accept it. Commented Nov 27, 2018 at 11:51

1 Answer 1

1

Depending on your applicaiton's context, .Wait() can cause a deadlock when your code reaches an async call.

You should be using .GetAwaiter().GetResult() so the state machine is properly generated behind the scenes to prevent this issue.

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

Comments

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.