jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
11.06k stars 1.17k forks source link

[Question] DbContextPooling issue with INotifications when publish is executed #1003

Closed Ryujose closed 5 months ago

Ryujose commented 7 months ago

Issue

We're having an issue with INotification event for domain events. The thing is that we were trying to use DbContextPooling but it seems that this throw an exception because a captive dependency https://blog.ploeh.dk/2014/06/02/captive-dependency/

App structure

Possible solution

We were thinking to resolve this registering the INotification on hand as a scoped service.

Possible impacts

You know if there could be some extra impact on this possible solution?

jbogard commented 7 months ago

What's wrong with the handler being a transient exactly?

Ryujose commented 7 months ago

The particular reason here is for this point.

https://learn.microsoft.com/en-us/ef/core/performance/advanced-performance-topics?tabs=with-di%2Cexpression-api-with-constant#managing-state-in-pooled-contexts

Context pooling works by reusing the same context instance across requests; this means that it's effectively registered as a Singleton, and the same instance is reused across multiple requests (or DI scopes). This means that special care must be taken when the context involves any state that may change between requests.

In A DDD structure for domain events we usually have some sequence like this.

-> Endpoint 
     -> Command handler
          -> Domain events registered
          -> UnitOfWork SaveChanges
               -> DomainEventDispatcher
                    -> INotification publish
               -> Entity Framework Core SaveChanges

Looking on this sequence the issue happens because we've the following on DI

  1. DbContextPooling is instanced as singleton
  2. Transient DI registration is causing distortions with this pool because each new instance is requested on each call inside the same request.

What were the issues we were having?

  1. An exception telling us that INotification should be scoped in our local environment

An unhandled exception has occurred while executing the request. System.InvalidOperationException: Cannot resolve 'System.Collections.Generic.IEnumerable1[MediatR.INotificationHandler1[Event registered]]' from root provider because it requires scoped service 'Random.Domain.Aggregates.RandomAggregate.IRandomRepository'. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(ServiceCallSite callSite, IServiceScope scope, IServiceScope rootScope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at MediatR.Wrappers.NotificationHandlerWrapperImpl1.Handle(INotification notification, IServiceProvider serviceFactory, Func4 publish, CancellationToken cancellationToken)

  1. No error were throwing but instead it was saving data inside the table tracked in command handler but not in the one that we were registering inside the domain handler after the publish.

Turning into the core question

There's some possible impact if we choose the path of trying to use scoped instead of transient for those calls?

github-actions[bot] commented 5 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] commented 5 months ago

This issue was closed because it has been stalled for 14 days with no activity.