注入依赖项的UserManager正在处理异步调用(ASP.NET CORE) [英] Dependency Injected UserManager is disposing on async call (ASP.NET CORE)

查看:306
本文介绍了注入依赖项的UserManager正在处理异步调用(ASP.NET CORE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Core及其内置的依赖项注入和asp.net身份.

我遇到的问题是,每当我尝试通过异步访问任何上下文(即在本示例中为UserManager<ApplicationUser>)时,访问时都会将其丢弃.

例如,我的控制器在下面(我们关注的对象是UserManager)

private readonly ApplicationDbContext _dbContext;
private readonly UserManager<ApplicationUser> _userManager;
private readonly ViewRender _view;
private readonly IConfiguration _config;

public UserController(ApplicationDbContext dbContext,
    UserManager<ApplicationUser> userManager,
    ViewRender view,
    IConfiguration config)
{
    this._dbContext = dbContext;
    this._userManager = userManager;
    this._view = view;
    this._config = config;
}

请注意,_userManager正在使用startup.cs中的Visual Studio模板默认设置进行注入

现在在此控制器中的方法中,我尝试以下操作:

[HttpDelete]
public async void Delete(string id)
{

    var user = _userManager.Users.Where(a => a.Id == id).FirstOrDefault();
    user.Deleted = true;
    var result= await _userManager.UpdateAsync(user);

}

,但是当按下await _userManager.UpdateAsync(user)时,它将调用对象已放置"错误.我对async/await的理解是执行将在await停止?因此,在完成_userManager对象之前,控制器不应该处置它. (显然,这种逻辑是错误的)

一种有效的解决方案是不使用aysnc方法,而仅调用_userManager.UpdateAsync(user).Result;,但希望了解aysnc/await

解决方案

将操作更新为具有Task返回类型而不是void:

[HttpDelete]
public async Task Delete(string id) {
    var user = _userManager.Users.Where(a => a.Id == id).FirstOrDefault();
    user.Deleted = true;
    var result= await _userManager.UpdateAsync(user);
}

为了更好地了解async/await,请阅读本文

异步/等待-异步编程的最佳做法

I am using ASP.NET Core with its inbuilt dependency injection and asp.net identity.

The problem I am having is whenever I try and access any of the context's via async (ie in this example UserManager<ApplicationUser>) it is being disposed when accessing.

For example, my controller is below (The object we are concerned with is UserManager)

private readonly ApplicationDbContext _dbContext;
private readonly UserManager<ApplicationUser> _userManager;
private readonly ViewRender _view;
private readonly IConfiguration _config;

public UserController(ApplicationDbContext dbContext,
    UserManager<ApplicationUser> userManager,
    ViewRender view,
    IConfiguration config)
{
    this._dbContext = dbContext;
    this._userManager = userManager;
    this._view = view;
    this._config = config;
}

Notice the _userManager is being injected using the visual studio template default setting in startup.cs

Now in a method in this controller I try the following:

[HttpDelete]
public async void Delete(string id)
{

    var user = _userManager.Users.Where(a => a.Id == id).FirstOrDefault();
    user.Deleted = true;
    var result= await _userManager.UpdateAsync(user);

}

but when the await _userManager.UpdateAsync(user) is hit, it calls a "the object is disposed" error. My understanding of async / await is that the execution will stop at await? Therefore the controller should not dispose _userManager object until its completed? (obviously this logic is wrong)

A solution which works is to not use a aysnc method and just call _userManager.UpdateAsync(user).Result; but would like to understand aysnc/await

解决方案

Update action to have Task return type instead of void:

[HttpDelete]
public async Task Delete(string id) {
    var user = _userManager.Users.Where(a => a.Id == id).FirstOrDefault();
    user.Deleted = true;
    var result= await _userManager.UpdateAsync(user);
}

For a better understanding of async/await read this article

Async/Await - Best Practices in Asynchronous Programming

这篇关于注入依赖项的UserManager正在处理异步调用(ASP.NET CORE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆