我有一个使用存储库(userRepo)的方法:
public override Task<IdentityResult> CreateLocalUserAsync(IUser user, string password, CancellationToken cancellationToken)
{
var task = new Task<IdentityResult>(() => {
TUserEntity newUser = new TUserEntity
{
Id = user.Id,
UserName = user.UserName,
Password = password
};
userRepo.Save(newUser).Flush();
return new IdentityResult(true);
}, cancellationToken);
task.Start();
return task;
}
该userRepo对象具有使用HttpContext.Current. 这两个都是使用 ninject 解决的InRequestScope。
AccountController上面的方法在 Mvc 5的默认内部调用:
var result = await IdentityManager.Users.CreateLocalUserAsync(user, model.Password);
我尝试将此设置添加到 web.config:
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
另外,我肯定在使用 .NET 4.5。这也在我的 web.config 中:
<httpRuntime targetFramework="4.5" />
在我开始任务之前无法获取信息,HttpContext因为任务中的依赖userRepo项正在使用信息,并且两个对象都使用 Ninject 解析。
我如何确保它HttpContext.Current不会为空?