.Net Core 2.1-无法访问已处置的对象。对象名称:“ IServiceProvider” [英] .Net Core 2.1 - Cannot access a disposed object.Object name: 'IServiceProvider'

查看:882
本文介绍了.Net Core 2.1-无法访问已处置的对象。对象名称:“ IServiceProvider”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将.NET Core 2.0迁移到.NET Core 2.1。一切正常,但是当我尝试立即登录时,出现以下错误:

I just migrated .NET Core 2.0 to .NET Core 2.1. Everything went fine, but when I try to login now I get the folowing error:



  • $ exception {System .ObjectDisposedException:无法访问已处置的对象。
    对象名称: IServiceProvider。

这发生在以下代码中:

This happens in this bit of code:

public class AppContractResolver : DefaultContractResolver
{

    private readonly IServiceProvider _services;

    public AppContractResolver(IServiceProvider services)
    {
        _services = services;
    }

    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var httpContextAccessor = _services.GetService<IHttpContextAccessor>();
        var user = httpContextAccessor.HttpContext.User;

        List<JsonProperty> properies = base.CreateProperties(type, memberSerialization).ToList();

        properies = FilterOneClaimGranted(type, properies, user);

        return properies;
    }

在此行上发生:

var httpContextAccessor = _services.GetService<IHttpContextAccessor>();

这确实适用于.NET Core 2.0

This did work on .NET Core 2.0

我尝试将 HttpContextAccessor 添加到我的启动中,但这没用。

I have tried adding the HttpContextAccessor to my startup, but that did not work.

所以,怎么办我可以解决这个问题吗?

So, how do I fix this?

让我知道您是否需要更多代码。我会很乐意提供更多,但我不知道您可能需要或不需要什么,因此我没有添加很多代码。'

Let me know if you need more code. I will happily provide more, but I don't know what you might or might not need, so therefor I did not add a lot of code.'

编辑

我已经将 services.AddHttpContextAccessor(); 添加到了我的启动程序中,但这并没有似乎有效。

I have added services.AddHttpContextAccessor(); to my startup, but that does not seem to work. Still getting the error.

编辑2:

完整的堆栈跟踪:

- $exception    {System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at WebAPI.Extensions.AppContractResolver.CreateProperties(Type type, MemberSerialization memberSerialization) in C:\Users\luukw\Desktop\stage\blacky-api\Blacky\Extensions\Resolver\AppContractResolver.cs:line 25
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Microsoft.AspNetCore.Mvc.Formatters.JsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)}    System.ObjectDisposedException


推荐答案

我建议不要调用
services.GetService< IHttpContextAccessor>(),将 IHttpContextAccessor 注入构造函数,并使用私有字段存储值

I would suggest that instead of calling services.GetService<IHttpContextAccessor>(), inject IHttpContextAccessor to the constructor and use aprivate field to store the value.

public AppContractResolver(IServiceProvider services, 
                           IHttpContextAccessor httpContextAccessor)
{
   _services = services;
   this.httpContextAccessor = httpContextAccessor;
}

还必须手动注册HttpContextAccessor。
Startup.cs 的RegisterServices中添加, services.AddSingleton< IHttpContextAccessor,HttpContextAccessor>();

Also HttpContextAccessor has to be registered manually. In RegisterServices in Startup.cs add, services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

这篇关于.Net Core 2.1-无法访问已处置的对象。对象名称:“ IServiceProvider”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆