gpt4 book ai didi

c# - .Net Core 2.1 - 无法访问已处置的对象。对象名称 : 'IServiceProvider'

转载 作者:IT王子 更新时间:2023-10-29 04:49:07 26 4
gpt4 key购买 nike

我刚刚将 .NET Core 2.0 迁移到了 .NET Core 2.1。一切顺利,但是当我现在尝试登录时,出现以下错误:

  • $exception {System.ObjectDisposedException: Cannot access a disposed object. Object name: 'IServiceProvider'.

这发生在这段代码中:

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

我曾尝试将 HttpContextAccessor 添加到我的启动项中,但没有成功。

那么,我该如何解决这个问题?

如果您需要更多代码,请告诉我。我很乐意提供更多,但我不知道您可能需要或不需要什么,因此我没有添加很多代码。”

编辑

我已将 services.AddHttpContextAccessor(); 添加到我的启动项中,但这似乎不起作用。仍然出现错误。

编辑 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

最佳答案

在我的案例中,问题在 Startup.cs

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

只需替换 services.GetService<>()app.ApplicationServices.GetService<>()

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

希望对你有帮助

关于c# - .Net Core 2.1 - 无法访问已处置的对象。对象名称 : 'IServiceProvider' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50619696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com