# - net - cannot access a disposed object r nobject name filebufferingreadstream
.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
# - net - cannot access a disposed object r nobject name filebufferingreadstream的更多相关文章
- Xamarin.Forms bug? System.ObjectDisposedException: Cannot access a disposed object
Hi, My Android Xamarin.Forms application uses a Navigation stack to display various views, I often h ...
- System.Net.Sockets.Socket SendAsync System.ObjectDisposedException: Cannot access a disposed object.
发生未处理的域异常! System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net ...
- asp.net Core HttpClient 出现Cannot access a disposed object. Object name: 'SocketsHttpHandler' 的问题。
ASP.NET Core 部署在Centos 中 偶尔出现 One or more errors occurred. (Cannot access a disposed object.Object n ...
- abp Cannot access a disposed object. A common cause of this error is disposing
框架:abp 异常信息: An unhandled exception was thrown by the application.System.ObjectDisposedException: Ca ...
- Cannot access a disposed object in ASP.NET Core
Cannot access a disposed object in ASP.NET Core 楠木大叔 导航 常见原因 总结 对于.neter来说,在使用ASP.NET Core的过程中 ...
- mac上的xampp出现Access forbidden! You don’t have permission to access the requested object. It is either
一个Joomla!程序,之前是在win上的xampp上运行得非常好的,当我把它拿到mac下面的xampp上去运行的时候,发现有问题,没法运行,报以下的错误: Access forbidden! Yo ...
- You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE
Symptoms When accessing an STL object created in one DLL or EXE through a pointer or reference in a ...
- Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server
好久不没弄 apache和php了,突然遇到这种奇葩的问题,本来想直接在网上找现成的解决思路,结果网上搜索花了不少功夫,也没找到原因. 后来看日志文件:apache\logs\error.log发现了 ...
- 平时Error记录
The Windows Firewall on this machine is currently 1.This row already belongs to another table. DataT ...
随机推荐
- Python运行语法错误:IndentationError: unindent does not match any outer indentation level
python脚本没有对齐.新的Python语法,是不支持的代码对齐中,混用TAB和空格的.
- Navicat自动断开连接处理方式
问题描述 使用Navicat连接mysql后,如果一段时间不操作,那么会再次操作时会提示无响应,每次都这样确实折磨人,大大降低了工作效率! 问题解决 关闭连接→右键连接→连接属性 将上述心跳时间设置为 ...
- c++笔试题 已迁移完成
转载 1.C和C++的特点与区别? 答:(1)C语言特点:1.作为一种面向过程的结构化语言,易于调试和维护: 2.表现能力和处理能力极强,可以直接访问内存的物理地址: 3.C语言实现了对硬件的编程操作 ...
- zabbix TCP 连接数监控
直接上配置: 1.修改配置 cat userparameter_tcp_connect.conf UserParameter=tcp_connect.established,/opt/app/zabb ...
- 三个面向对象相关的装饰器@property@staticmathod@classmethod
@property 先看实例: from math import pi class Circle: def __init__(self,r): self.r = r @property def per ...
- [Beta阶段]第九次Scrum Meeting
Scrum Meeting博客目录 [Beta阶段]第九次Scrum Meeting 基本信息 名称 时间 地点 时长 第九次Scrum Meeting 19/05/15 大运村寝室6楼 40min ...
- 完全卸载Oracle(亲身体验完整版)
完全卸载Oracle 用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,那么怎么才能完全卸载Oracle呢? 那就是直接注册表清除,步骤如下: 开始->设置-& ...
- 【转】Android root检测方法总结
一 为什么要进行root检测?出于安全原因,我们的应用程序不建议在已经root的设备上运行,所以需要检测是否设备已经root,以提示用户若继续使用会存在风险. 二 root了会有什么风险?在Linux ...
- Linux下的sleep()和sched_yield()(转)
阿里四面被问到了这个问题,一脸懵逼,下来也没找到什么阐述这个的文章,就自己查man来对比总结一下吧: sched_yield()的man手册描述如下: DESCRIPTION sched_ ...
- regexp_replace
pandas和SQL数据分析实战 https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2&sha ...