Comments (4)
What's wrong with the handler being a transient exactly?
from mediatr.
The particular reason here is for this point.
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
- DbContextPooling is instanced as singleton
- 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?
- 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.IEnumerable
1[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.NotificationHandlerWrapperImpl
1.Handle(INotification notification, IServiceProvider serviceFactory, Func4 publish, CancellationToken cancellationToken)
- 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?
from mediatr.
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.
from mediatr.
This issue was closed because it has been stalled for 14 days with no activity.
from mediatr.
Related Issues (20)
- Improve base package readme
- Implement IRequest, how to enter Behavior HOT 1
- IRequestExceptionHandler from other project not loaded
- Question:
- Question: How I can setup MediatR 12.1.0 in Ninject module? HOT 2
- How do I register INotification with generics? HOT 2
- long running INotificationHandler HOT 3
- Question: Can multiple MediatR handlers be packaged into a single class HOT 3
- .NET8 upgrade from .NET Core 2.1 : Command cannot be used as type parameter 'TRequest' for 'IRequestHandler<TRequest>' HOT 3
- Irequest in library and handler in web api project HOT 2
- what use net8 aot publish HOT 2
- v12.3.0 Breaking Change HOT 27
- Question: Why IRequestExceptionHandler is not catching? HOT 1
- Question: How to register generic requests HOT 8
- Can't register generic handlers in latest version HOT 5
- Question: Why MediatR does not support ValueTasks ?
- Question: Why MediatR does not support ValueTasks ? HOT 2
- Generic Handler implementation only handles single generic argument and breaks existing implementations HOT 9
- Validation is not invoked when IRequest has no Result
- Handlers for interfaced notifications are not working as expected
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mediatr.