身份框架中是否有 GetUserIdAsync()?

Is there a GetUserIdAsync() in Identity Framework?

最初我在 MVC Razor 应用程序中遇到以下错误(在完成加载页面之前单击某些选项时):

A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.

所以我开始按照 MS 的建议使用 异步方法。这涉及在多个控制器操作中级联异步更改(实施 async 在传播方式上被比作 "Zombie Virus",我倾向于同意)。

我仍然在注入的 CurrentCandidate class:

中使用以下异步方法时遇到该错误
    public async Task<ApplicationUser> ApplicationUserAsync()
    {
        var applicationUser = await this._userManager.FindByIdAsync(HttpContext.Current.User.Identity.GetUserId());
        return applicationUser;
    }

大概是因为 HttpContext.Current.User.Identity.GetUserId() 是 运行 一些异步的东西 在幕后

我假设,因为它是一个 MS 库,所以会有 GetUserId() 的异步版本,但我找不到一个(或任何说明它存在的文档)

我在这里有哪些选择?我在同事的压力下完全放弃 async 方法,但这只会导致在另一个地方出现同样的错误。

建议?

更新:

将两个调用分开成不同的行可以使画面更清晰。崩溃发生在 FindByIdAsync():

    public async Task<ApplicationUser> ApplicationUserAsync()
    {
        var id = HttpContext.Current.User.Identity.GetUserId();
        var applicationUser = await this._userManager.FindByIdAsync(id); <<<< crashes here
        return applicationUser;
    }

HttpContext.Current.User.Identity.GetUserId() 没有异步版本。 GetUserId 未连接到 EF 或任何异步操作。这是源代码:https://aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.Core/Extensions/IdentityExtensions.cs 它所做的只是进入 cookie 数据并获取与 UserId 相关的声明值。

所以您需要找到导致问题的其他操作。