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.

INotificationHandler<T> where T is Interface does not handle publish calls

See original GitHub issue
public 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:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jbogardcommented, Aug 13, 2020

Feel free to upvote this PR:

https://github.com/dotnet/runtime/pull/39540

That adds constrained generics support to the built-in DI container.

1reaction
ArrowtheArchercommented, Aug 13, 2020

Their relationship

    public class Notification : ISubNotification
    {
        public string Par { get; set; }

        public Notification(string par)
        {
            Par = par;
        }
    }

    public interface ISubNotification : INotification
    {
        string Par { get; set; }
    }

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 error


System.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()
Read more comments on GitHub >

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

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