Open bzbetty opened 2 months ago
Adding the factory as transient instead of singleton may have fixed the issue.
Happened again, could be because I changed to a pool db factory (which has to be singleton) or perhaps I never fixed the issue
hmm I seem to also be able to get "SaveChangesWithoutTriggersAsync(), but it throws this exception 'System.InvalidOperationException: 'A triggerSession has already been created''!" occasionally too.
I think there's definitely some threadsafety or leakage issues happening for some reason.
Starting to think maybe Azure Functions (or my project) has something very different about it.
Added the EFCore Triggerred Source to my project and I'm hitting some rather weird Debug.Asserts
wonder if it's due to TriggerSessionSaveChangesInterceptor being registered against the db factory like I did, that might effectively make it shared?
yup, seems that was it!
using AddInterceptors like this is bad
opt.UseSqlServer()
.UseTriggers()
.AddInterceptors(triggerInterceptor);
as it means it shares the one interceptor between all dbcontexts (threading issue).
adding it in the dbcontext in onconfiguring seems to work
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.AddInterceptors(new TriggerSessionSaveChangesInterceptor());
base.OnConfiguring(optionsBuilder);
}
(still no idea why i had to add it myself, but triggers didn't fire without it)
That said you can't use OnConfiguring if Db Pooling is enabled :(
System.InvalidOperationException: 'OnConfiguring' cannot be used to modify DbContextOptions when DbContext pooling is enabled.
Curiously I think for the Db Pooling, it might actually work better to use the v2 mode where you extend the dbcontext instead.
May be a good reason not to remove it in v4?
also recommend against using
services.AddDbContext
syntax as it does an assembly scan each context cretaion (50ms for me).
Occasionally hit the following which kills our app.
using latest version 3.
We've only recently implemented EFC Triggered, so we may have done something wrong - this issue is just in case you know about something, otherwise if we figure out what we did wrong we'll add to document it.
potentially related to #124?
we did add the dbcontextfactory by copying code from https://github.com/koenbeuk/EntityFrameworkCore.Triggered/pull/195
being called in program.cs (Azure Functions, dotnet 8, isolated)
Also saw the following stackoverflow post which suggests it may be better to inject
IServiceScopeFactory
instead ofIServiceProvider
, which might make sense if there's a singleton object somewhere, but i haven't seen one.https://stackoverflow.com/questions/76745332/c-sharp-timer-cannot-access-a-disposed-object-object-name-iserviceprovider