相关疑难解决方法(0)

"对象引用未设置为对象的实例"是什么意思?

我收到此错误,我不确定这是什么意思?

你调用的对象是空的.

.net nullreferenceexception

182
推荐指数
5
解决办法
127万
查看次数

异步任务中的HttpContext.Current null

我有一个使用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;
    }
Run Code Online (Sandbox Code Playgroud)

userRepo对象具有使用的依赖项HttpContext.Current.这两个都是使用ninject解决的InRequestScope.

AccountController在Mvc 5中默认调用上面的方法:

var result = await IdentityManager.Users.CreateLocalUserAsync(user, model.Password);
Run Code Online (Sandbox Code Playgroud)

我尝试将此设置添加到web.config:

<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
Run Code Online (Sandbox Code Playgroud)

另外,我肯定使用的是.NET 4.5.这也在我的web.config中:

<httpRuntime targetFramework="4.5" />
Run Code Online (Sandbox Code Playgroud)

HttpContext在我开始执行任务之前无法获取信息,因为任务中的依赖userRepo项正在使用信息,并且使用Ninject解析了这两个对象.

我怎样才能确保HttpContext.Current不会为空?

c# ninject httpcontext async-await

11
推荐指数
1
解决办法
9516
查看次数