.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的更多相关文章

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

  2. System.Net.Sockets.Socket SendAsync System.ObjectDisposedException: Cannot access a disposed object.

    发生未处理的域异常! System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net ...

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

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

  5. Cannot access a disposed object in ASP.NET Core

    Cannot access a disposed object in ASP.NET Core 楠木大叔     导航 常见原因 总结   对于.neter来说,在使用ASP.NET Core的过程中 ...

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

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

  8. 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发现了 ...

  9. 平时Error记录

    The Windows Firewall on this machine is currently 1.This row already belongs to another table. DataT ...

随机推荐

  1. Sublime Text 3.2.1详细安装破解教程,附最新激活码license(全网独家可用有效)

    title: "Sublime Text 3.2.1详细安装破解教程,附最新激活码license(全网独家可用有效)" categories: soft tags: soft au ...

  2. 个人收集的Android开源项目

    1. KnowWeather 下载:GitHub 一款 Android 开源天气 App ,包含天气信息.详情.生活指数等,通知栏,桌面小部件,定时更新天气等等,应用没有任何广告,支持县级.区级城市的 ...

  3. Openresty与Tengine

    Tengine官方网站:http://tengine.taobao.org/index_cn.html OpenResty官方网站:http://openresty.org/ Openresty和Te ...

  4. LiveCD 修复Grub引导

    /usr/sbin/grub-probe: error: failed to get canonical path of /cow 如果你是从这句报错search过来的,恭喜您!看来你也是一个调皮的孩 ...

  5. ubuntu 16.04 安装teamviewer

    很多人可能会问,为什么要在ubuntu上安装teamview?shell不就够用了吗?但实际上,很多时候,在远程连接linux的时候,我们需要在图形用户界面上进行操作.现在我就遇到了一个实际的问题:每 ...

  6. HDFS java API TROUBLESHOOTING

    官方文档:https://hadoop.apache.org/docs/r2.9.2/hadoop-project-dist/hadoop-common/SingleCluster.html 配置免密 ...

  7. VS找不到头文件但是系统路径已经设置

    有时候会出现打不开Include文件的问题,但是在系统包含include路径中已经设置,编译也没问题,就是一直有错误提示 这时候可以尝试在项目上点击右键,选择“下载项目”,然后再选择加载 如果还是不行 ...

  8. Ant Design Pro 子界面传值

  9. 升级到11.2.0.4后用srvctl无法启用数据库实例,报CRS-0254: authorization failure

    在standby database上从11.2.0.3升级11.2.0.4,然后打了补丁PATCH SET UPDATE 11.2.0.4.190115后,无法用srvctl启动第二个节点数据库实例: ...

  10. Session覆盖测试(要验证码提交到后续页面操作的 绕过去的场景)

    测试原理和方法 找回密码逻辑漏洞测试中也会遇到参数不可控的情况,比如要修改的用户名或者绑定 的手机号无法在提交参数时修改,服务端通过读取当前session会话来判断要修改密码的账 号,这种情况下能否对 ...