I have a method that uses a repository (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; } The userRepoobject has a dependency that uses HttpContext.Current. Both of these are resolved using ninject InRequestScope.
The above method is called inside the default AccountController in Mvc 5:
var result = await IdentityManager.Users.CreateLocalUserAsync(user, model.Password); I have tried adding this setting to web.config:
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> Also, I am definitely using .NET 4.5. This is also in my web.config:
<httpRuntime targetFramework="4.5" /> It is not possible to get the information from the HttpContext before I start the task because a dependency of the userRepo in the task is using the information and both objects are resolved using Ninject.
How can I ensure that HttpContext.Current will not be null?
HttpContext.Currentwill not be null?” Don't useTasks orasync-awaitat all. It doesn't improve anything in this case.awaitis http-context aware, at least; the problem here is thatTask.Startisn't.