ASP.NET C# Azure documentDb class 库不工作

ASP.NET C# Azure documentDb class library not working

我已经按照 MSDN 指南构建了一个简单的应用程序,以使用存储在 Azure 中的示例 documentDB。

从我的控制台应用程序我可以运行这个方法(在class库项目中)并且一切正常:

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;
        }
    }
}

但是当我从 ASP.NET 应用程序使用相同的 class 库时,程序在 ReadDatabaseAsync 方法调用时停止(并保持静止,没有异常)。

我已经尝试更改异步调用...但没有成功。

Gabi在上面的评论中回答了,所以在这里只是将其转换为答案,以便于发现。 This video by the ASP.NET team has good information on using async in ASP.NET。 最好完全异步(将调用代码更改为等待)。在 ASP.NET 时不要调用 .Wait()。

但是如果出于某种原因你必须 Wait(),请在上面的代码中对异步调用使用 .ConfigureAwait(false),例如

等待client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId)).ConfigureAwait(false);