question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF Interceptor should not crash the workflow if something fails during audit

See original GitHub issue

Describe the bug Right now I have two problems:

  1. EF interceptor is crashing, and I need to analyze why it’s crashing
  2. 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

  1. It should not fail
  2. 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:closed
  • Created 6 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thepirat000commented, Mar 27, 2023

This was fixed in version 20.2.4, please upgrade your references and re-test

0reactions
thepirat000commented, Mar 26, 2023

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found