Jack
Jack

Reputation: 538

ASP.NET C# Azure documentDb class library not working

I've built a simple app, following the MSDN guide, to use a sample documentDB stored in Azure.

From my console application i can run this method (in a class library project) and everything is ok:

private static async Task CreateDatabaseIfNotExistsAsync(string DatabaseId)
{
    try
    {
        await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId));
    }
    catch (DocumentClientException e)
    {
        if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
        {
            await client.CreateDatabaseAsync(new Database { Id = DatabaseId });
        }
        else
        {
            throw;
        }
    }
}

But when I use the same class library from an ASP.NET application the program stops (and stay still, no exception or else) on the ReadDatabaseAsync method call.

I've already tried to change the async calls...with no luck.

Upvotes: 1

Views: 441

Answers (1)

Kirill Gavrylyuk
Kirill Gavrylyuk

Reputation: 246

Gabi answered in the above comment, so just converting it to an answer here to make it easier to discover. This video by the ASP.NET team has good information on using async in ASP.NET. It is best to go fully async (change the calling code to await). Do not call .Wait() when in ASP.NET.

But if for some reason you must Wait(), use .ConfigureAwait(false) for the async calls in your code above, e.g.

await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId)).ConfigureAwait(false);

Upvotes: 1

Related Questions