INotificationHandler<T> where T is Interface does not handle publish calls
See original GitHub issuepublic class SubNotificationHandler : INotificationHandler<ISubNotification>
{
public async Task Handle(ISubNotification notification, CancellationToken cancellationToken)
{
//Do some
}
}
public class NotificationHandler : INotificationHandler<Notification>
{
public async Task Handle(Notification notification, CancellationToken cancellationToken)
{
//Do some
}
}
I was trying to do something like this and on publish both Handle methods are called but if NotificationHandler does not exist then SubNotificationHandler.Handler is not called. Is there any way to fix this without creating an empty NotificationHandler? (Notification class is an implementation of ISubNotification)
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
MediatR doesn't handle contravariant notifications
I went to source code of MediatR and it cannot resolve dependency INotificationHandler<SpecificAuditRequestBase> . But if instead of one handler ...
Read more >Diagnosing and Fixing MediatR Container Issues
We have a problem here - how do we assert that our handler was actually called? Publish returns only Task , there's nothing...
Read more >We Can Finally Publish Notifications In Parallel With MediatR 12
Check out my Pragmatic Clean Architecture course: https://www.milanjovanovic.tech/pragmatic-clean-architecture Support me on Patreon to ...
Read more >NET Core MediatR with Notification(Publish) and Behaviors
Simply put, this behavior can operate on any request. We then implement the Handle method, logging before and after we call the next()...
Read more >Enterprise PeopleTools 8.51 PeopleBook: Integration Broker
Implement the OnNotify method in the INotificationHandler application interface. To get content data out of a request message, use the following guidelines.
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
Feel free to upvote this PR:
https://github.com/dotnet/runtime/pull/39540
That adds constrained generics support to the built-in DI container.
Their relationship
I tried constrained generic(if I am not mistaking) like this
public class SubNotificationHandler<T> : INotificationHandler<T> where T: ISubNotification
and it did help this case but I later encountered a problem. When I try to publish a notification that is not an implementation of ISubNotification I am getting this errorSystem.ArgumentException: GenericArguments[0], 'Mediator_Test.Shared.SecondNotification', on 'Mediator_Test.Shared.SubNotificationHandler`1[T]' violates the constraint of type 'T'. ---> System.TypeLoadException: GenericArguments[0], 'Mediator_Test.Shared.SecondNotification', on 'Mediator_Test.Shared.SubNotificationHandler`1[T]' violates the constraint of type parameter 'T'. at System.RuntimeTypeHandle.Instantiate(Type[] inst) at System.RuntimeType.MakeGenericType(Type[] instantiation) --- End of inner exception stack trace --- at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e) at System.RuntimeType.MakeGenericType(Type[] instantiation) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateOpenGeneric(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateEnumerable(Type serviceType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.<>c__DisplayClass7_0.<GetCallSite>b__0(Type type) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at MediatR.ServiceFactoryExtensions.GetInstances[T](ServiceFactory factory) at MediatR.Internal.NotificationHandlerWrapperImpl`1.Handle(INotification notification, CancellationToken cancellationToken, ServiceFactory serviceFactory, Func`4 publish) at MediatR.Mediator.PublishNotification(INotification notification, CancellationToken cancellationToken) at MediatR.Mediator.Publish[TNotification](TNotification notification, CancellationToken cancellationToken) at Mediator_Test.Server.Controllers.TargetController.Index() in C:\Users\Nadir_A\source\repos\Mediator Test\Mediator Test\Server\Controllers\TargetController.cs:line 27 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()