.Net Core 2.1-Cannot access a disposed object.Object name: 'IServiceProvider' (3)

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: Cannot access a disposed object. Object name: '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;
}

It happens on this line:

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

This did work on .NET Core 2.0

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.'

EDIT

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

EDIT 2:

Full stacktrace:

- $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

 

For me it works with:

 public void ConfigureServices(IServiceCollection services)
{

services.AddHttpContextAccessor();

}

and then:

     public void Configure(IApplicationBuilder app, IHttpContextAccessor accessor)
{
...
...accessor.HttpContext.RequestService ... }

 

I would imagine that the IServiceProvider implementation may have been used in a using statement inadvertently somewhere or been disposed outright.

To test if this is the case you could try to resolve the IHttpContextAccessor right after, or in, the ConfigureServices method.

If it resolves there you would need to step through to find out where the IServiceProvider is being disposed.


 

In my case issue was in Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider services)
{
var svc = services.GetService<IServiceProvider>(); // <-- exception here
}

just replace services.GetService<>() with app.ApplicationServices.GetService<>()

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var svc = app.ApplicationServices.GetService<IServiceProvider>(); // no exception
}

hope it helps

最新文章

  1. 基于 SailingEase WinForm Framework 开发优秀的客户端应用程序(目录)
  2. 理解 neutron(15):Neutron linux-bridge-agent 创建 linux bridge 的简要过程
  3. setTimeout传递参数
  4. sqldatasource控件设置where语句
  5. C#之 HashSet(临时笔记,未参考资料,请慎重)
  6. centos nginx install openssl
  7. SharePoint2010沙盒解决方案基础开发——关于TreeView树形控件读取列表数据(树形导航)的webpart开发及问题
  8. 用css3实现各种图标效果
  9. 关于Grunt可视化的尝试
  10. nodejs笔记1 ----关于express不是本地命令
  11. [poj3565]Ants
  12. 如何在Cocos2D游戏中实现A*寻路算法(六)
  13. mysql error(2003) 10060的再解决
  14. Asp.net core中实现自动更新的Option
  15. nginx解决前端跨域配置
  16. 分享一些 Java 无关基础方面的书籍
  17. go 接口以及对象的使用
  18. tcp连接的状态变迁以及如何调整tcp连接中处于time_wait的时间
  19. Android 获取模拟器与真机数据库
  20. Solr7.4的学习与使用

热门文章

  1. shell 空语句
  2. ubuntu之路——day18 用pytorch完成CNN
  3. [Beta阶段]第四次Scrum Meeting
  4. [Gamma阶段]第一次Scrum Meeting
  5. 第08组 Beta冲刺(3/4)
  6. 深度排序模型概述(一)Wide&amp;Deep/xDeepFM
  7. Torch-Models 别人训练的FastNeuralStyle
  8. HandlerMethodArgumentResolver的抽象實現AbstractNamedValueMethodArgumentResolver下的子类
  9. SQL优化关于or与in使用
  10. 【GMT43智能液晶模块】例程十四:MODBUS TCP实验——电源监控