I'm wrapping AspNet.Identity. But something confuses me about TPL.
First example:
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();
}
Second example:
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();
}
And client will call this like:
result = await _userManager.RemovePasswordAsync(user.Id);
My first question is:
When the client calls the second method, the work is offloaded to a threadpool thread from the IO thread. And when RemovePasswordAsync
is called it calls UpdateAsync
which has an await keyword. So, at that point does this threadpool thread offload to another threadpool thread? Or does TPL continue using the same thread instead?
And my second question is; what is the main difference between the first implementation and the second implementation of constructing this async
method?
EDIT:
This is the update method of the UserStore
class. (_store.UpdateAsync(user)
)
public Task UpdateAsync(TUser user)
{
if (user == null)
throw new ArgumentNullException("user");
return _userService.UpdateAsync(user);
}
And this is the update method of the UserService
class
public Task UpdateAsync(TUser user)
{
return Task.Factory.StartNew(() => Update(user));
}