【发布时间】:2021-08-19 23:41:41
【问题描述】:
我正在使用 Entity Framework Core 编写一个 ASP.NET Core 应用程序。我也在使用 MediatR。
当我触发数据库更新时,无论是保存还是删除,我都会收到此错误:
无法访问已处置的对象。对象名称:'IServiceProvider'
有时会在第一次尝试时发生,有时在后续尝试时发生。我似乎找不到模式。
我设法做的是在调用await _applicationDbContext.SaveChangesAsync(cancellationToken); 的处理程序中打断点,尽管错误实际上是在await _mediator.Send(someRequestModel); 上的控制器中抛出的。引发错误后,应用程序进入中断模式并崩溃。
我将使用虚拟名称,但我认为这是相关代码:
控制器:
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
非常感谢任何帮助。
【问题讨论】:
-
Teun van Schagen 是正确的:
"Avoid Async Void"。这是一篇解释“为什么”的好文章:MSDN: Async/Await - Best Practices in Asynchronous Programming。请确认将控制器的签名更改为public async Task Delete(string id)可以解决问题;请投票并“接受”Teun van Schagen 的回复 :) -
我当然接受了!谢谢你的文章。
标签: c# asp.net-core dependency-injection mediatr