gpt4 book ai didi

c# - 无法访问已处置的对象。对象名称 : 'IServiceProvider' error in AspNet Core/EF Core project

转载 作者:行者123 更新时间:2023-12-05 00:53:27 27 4
gpt4 key购买 nike

我正在使用 Entity Framework Core 编写一个 ASP.NET Core 应用程序。我也在使用 MediatR。

当我触发数据库更新时,无论是保存还是删除,我都会收到此错误:

Cannot access a disposed object. Object name: 'IServiceProvider'

有时会在第一次尝试时发生,有时在后续尝试时发生。我似乎找不到模式。我确实设法做的是在调用 await _applicationDbContext.SaveChangesAsync(cancellationToken); 的处理程序中打断点,尽管错误实际上是在 Controller 中引发的,在 await _mediator.Send(someRequestModel); 。引发错误后,应用程序进入中断模式并崩溃。

我将使用虚拟名称,但我认为这是相关代码:

Controller :

public MyController(IMediator mediator)
{
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
}

[HttpDelete("{id}")]
public async void Delete(string id)
{
await _mediator.Send(new DeleteRequestModel(Guid.Parse(id))); // error thrown here
}

处理程序:

public class DeleteCommandHandler : IRequestHandler<DeleteRequestModel>
{
private readonly ApplicationDbContext _applicationDbContext;

public DeleteCommandHandler(ApplicationDbContext applicationDbContext)
{
_applicationDbContext = applicationDbContext;
}

public async Task<Unit> Handle(DeleteRequestModel request, CancellationToken cancellationToken)
{
var item = _applicationDbContext.MyData.First(x => x.Id == request.Id);
_applicationDbContext.MyData.Remove(item);

await _applicationDbContext.SaveChangesAsync(cancellationToken); // pretty sure this errors out

return Unit.Value;
}
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionString")));
services.AddDbContext<ApplicationAspNetUsersDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionString")));

services.AddDatabaseDeveloperPageExceptionFilter();

services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationAspNetUsersDbContext>();

services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationAspNetUsersDbContext>();

services.AddAuthentication()
.AddIdentityServerJwt();
services.AddControllers();
services.AddRazorPages();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; });

services.AddSwaggerDocument();

services.AddMediatR(AppDomain.CurrentDomain.Load("MySolution.MyProject.EntityFramework"));
services.AddValidatorsFromAssembly(AppDomain.CurrentDomain.Load("MySolution.MyProject"));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidatorPipelineBehavior<,>));

services.AddSingleton(x =>
new BlobServiceClient(Configuration.GetConnectionString("AzureBlobStorageConnection")));
services.AddApplicationInsightsTelemetry(Configuration["APPINSIGHTS_CONNECTIONSTRING"]);

services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
services.Configure<AzureBlobStorage>(Configuration.GetSection("AzureBlobStorage"));
services.Configure<ApplicationInsights>(Configuration.GetSection("ApplicationInsights"));
}

最后,这是堆栈跟踪,这次是更新操作:

at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at MediatR.Pipeline.RequestExceptionActionProcessorBehavior`2.GetActionsForException(Type exceptionType, TRequest request, MethodInfo& actionMethodInfo)
at MediatR.Pipeline.RequestExceptionActionProcessorBehavior`2.<Handle>d__2.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MediatR.Pipeline.RequestPostProcessorBehavior`2.<Handle>d__2.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MediatR.Pipeline.RequestPreProcessorBehavior`2.<Handle>d__2.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MySolution.Web.Controllers.MyController.< Patch >d__7.MoveNext() in
..\Controllers\MyController.cs:line 85

非常感谢任何帮助。

最佳答案

您在 Controller 中使用了异步 void。 Async void 应该只在特定场景中使用,在这种情况下,您应该使用 Task 作为返回类型。

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

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