我正在封装AspNet.Identity。但是TPL让我感到困惑。
第一个例子:
public virtual async Task<IdentityResult> RemovePasswordAsync(string userId)
{
var user = _store.FindByIdAsync(userId).Result;
if (user == null)
throw new InstanceNotFoundException("user");
user.PasswordHash = String.Empty;
user.SecurityStamp = String.Empty;
return await UpdateAsync(user);
}
public virtual async Task<IdentityResult> UpdateAsync(TUser user)
{
await _store.UpdateAsync(user);
return new IdentityResult();
}
第二个例子:
public virtual Task<IdentityResult> RemovePasswordAsync(string userId)
{
var user = _store.FindByIdAsync(userId).Result;
if (user == null)
throw new InstanceNotFoundException("user");
user.PasswordHash = String.Empty;
user.SecurityStamp = String.Empty;
return UpdateAsync(user);
}
public virtual async Task<IdentityResult> UpdateAsync(TUser user)
{
await _store.UpdateAsync(user);
return new IdentityResult();
}
客户端将这样进行调用:
result = await _userManager.RemovePasswordAsync(user.Id);
我的第一个问题是:
当客户端调用第二个方法时,工作会从IO线程转移到线程池线程。而当调用RemovePasswordAsync
时,它会调用UpdateAsync
,其中包含一个await关键字。那么,在那一点上,这个线程池线程是否会转移到另一个线程池线程?还是TPL继续使用相同的线程?
我的第二个问题是:构建这个async
方法的第一种实现和第二种实现之间的主要区别是什么?
编辑:
这是UserStore
类的更新方法。(_store.UpdateAsync(user)
)
public Task UpdateAsync(TUser user)
{
if (user == null)
throw new ArgumentNullException("user");
return _userService.UpdateAsync(user);
}
这是UserService
类的更新方法
public Task UpdateAsync(TUser user)
{
return Task.Factory.StartNew(() => Update(user));
}