EF Interceptor should not crash the workflow if something fails during audit
See original GitHub issueDescribe the bug Right now I have two problems:
- EF interceptor is crashing, and I need to analyze why it’s crashing
- BUT, when EF interceptor crashes, the workflow stops, IMHO audit should be “invisible”, what do you think?
To Reproduce
- add AuditSaveChangesInterceptor
- method that creates dbContext using injected dbcontextpooledfactory and when it save it will fail
await using var dbContext = await _stammdatenDbContextFactory.CreateDbContextAsync();
// do stuff
await dbContext.SaveChangesAsync(); <-- save fails for exceptions
System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'StammdatenDbContext'.
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed() at Microsoft.EntityFrameworkCore.DbContext.get_Database() at Audit.EntityFramework.DbContextHelper.IsRelational(DbContext dbContext) at Audit.EntityFramework.DbContextHelper.GetClientConnectionId(DbContext dbContext) at Audit.EntityFramework.DbContextHelper.UpdateAuditEvent(EntityFrameworkEvent efEvent, IAuditDbContext context) at Audit.EntityFramework.DbContextHelper.SaveScopeAsync(IAuditDbContext context, IAuditScope scope, EntityFrameworkEvent event) at Audit.EntityFramework.DbContextHelper.EndSaveChangesAsync(IAuditDbContext context, IAuditScope scope, Int32 result, Exception exception) at Audit.EntityFramework.AuditSaveChangesInterceptor.SaveChangesFailedAsync(DbContextErrorEventData eventData, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
Expected behavior
- It should not fail
- it should not crash the workflow when it fails
Libraries (specify the Audit.NET extensions being used including version):
- Audit.EntityFramework.Core: 20.1.3
Target .NET framework:
- .NET 6
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created 6 months ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Interceptors - EF Core
The audit entity is stored on the interceptor so that it can be accessed again once SaveChanges either succeeds or fails. For success, ......
Read more >Add interception event to be fired when EF has finished ...
I have attempted to build both an IDbConnectionInterceptor or IDbCommandInterceptor implementation that allows me to attach to the underlying ...
Read more >Using interceptors to find slow queries in Entity Framework
Here's a quick way to find slow queries in EF ... By using Entity Frameworks interceptor system and hooking into the ReaderExecuted event...
Read more >How to offload EF Core entity auditing to a different thread ...
I'm building an entity auditing solution using EF Core change tracking. I'm able to do it by overriding the SaveChangesAsync method and ...
Read more >Entity Framework Core Extension tips & tricks - Injecting ...
Entity Framework Core Extension tips & tricks - Injecting dependencies into EF Interceptors.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This was fixed in version 20.2.4, please upgrade your references and re-test
You’re right, in that case, I think the audit should not throw.
I would work on the fix to allow continuing with the audit process even if the DbContext got disposed of within the SaveChanges operation.