title: .Net Core 2.1 - Cannot access a disposed object.Object name: 'IServiceProvider'
body: <p>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:</p>
  
  <blockquote>
    <ul>
    <li>$exception  {System.ObjectDisposedException: Cannot access a disposed object.
    Object name: 'IServiceProvider'.</li>
    </ul>
  </blockquote>
  
  <p>This happens in this bit of code:</p>
  
  <pre><code>public class AppContractResolver : DefaultContractResolver
  {
  
      private readonly IServiceProvider _services;
  
      public AppContractResolver(IServiceProvider services)
      {
          _services = services;
      }
  
      protected override IList&lt;JsonProperty&gt; CreateProperties(Type type, MemberSerialization memberSerialization)
      {
          var httpContextAccessor = _services.GetService&lt;IHttpContextAccessor&gt;();
          var user = httpContextAccessor.HttpContext.User;
  
          List&lt;JsonProperty&gt; properies = base.CreateProperties(type, memberSerialization).ToList();
  
          properies = FilterOneClaimGranted(type, properies, user);
  
          return properies;
      }
  </code></pre>
  
  <p>It happens on this line:</p>
  
  <pre><code>var httpContextAccessor = _services.GetService&lt;IHttpContextAccessor&gt;();
  </code></pre>
  
  <p>This did work on .NET Core 2.0 </p>
  
  <p>I have tried adding the <code>HttpContextAccessor</code> to my startup, but that did not work.</p>
  
  <p>So, how do I fix this?</p>
  
  <p>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.'</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>I have added <code>services.AddHttpContextAccessor();</code> to my startup, but that does not seem to work. Still getting the error.</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Full stacktrace:</p>
  
  <pre><code>- $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
  </code></pre>