public async Task BaseTest() { var serviceProvider = BuildServiceProvider(); var messageDispatcher = new MessageDispatcher(serviceProvider); // Register a handler for the message type var handler = new TestMessageHandler(); var handlerProvider = ContextualProvider.Create(handler); messageDispatcher.Register(handlerProvider); var handler2 = new TestMessageHandler(); messageDispatcher.Register(ContextualProvider.Create(handler2)); var baseHandler = new TestMessageBaseHandler(); messageDispatcher.Register(ContextualProvider.Create(baseHandler)); // If the handler (provider) is registered again, the order of handlers shall stay the same. messageDispatcher.Register(handlerProvider); var message = new TestMessage("x", 123); var dispatchResult = await messageDispatcher.DispatchAsync(message, new DispatchValueDictionary(), publish : false, cancellation : default); Assert.IsTrue(dispatchResult.IsSuccess); Assert.IsNull(handler.Message); // Handler shall not be called Assert.AreEqual(message, handler2.Message); // Handler2 shall be called Assert.IsNull(baseHandler.Message); // BaseHandler shall not be called }
public async Task PublishTest() { var serviceProvider = BuildServiceProvider(); var messageDispatcher = new MessageDispatcher(serviceProvider); // Register a handler for the message type var handler = new TestMessageHandler(); messageDispatcher.Register(ContextualProvider.Create(handler)); var handler2 = new TestMessageHandler(); messageDispatcher.Register(ContextualProvider.Create(handler2)); var baseHandler = new TestMessageBaseHandler(); messageDispatcher.Register(ContextualProvider.Create(baseHandler)); var message = new TestMessage("x", 123); var dispatchResult = await messageDispatcher.DispatchAsync(message, new DispatchValueDictionary(), publish : true, cancellation : default); Assert.IsTrue(dispatchResult.IsSuccess); Assert.AreEqual(message, handler.Message); Assert.AreEqual(message, handler2.Message); Assert.AreEqual(message, baseHandler.Message); }
public async Task <IHandlerRegistration> RegisterAsync(IContextualProvider <IMessageHandler <TMessage> > handlerProvider) { var provider = new ContextualProvider <IMessageReplyer <TMessage> >( serviceProvider => new MessageReplyer <TMessage>(_endPoint, handlerProvider.ProvideInstance(serviceProvider))); return(await HandlerRegistration.CreateRegistrationAsync(_handler, provider)); }
private static void AddMessageProcessors(IServiceCollection services) { services.Configure <MessagingOptions>(options => { options.MessageProcessors.Add(ContextualProvider.Create <EntityMessageHandlerProcessor>()); }); }
public static Task <IHandlerRegistration <IEventHandler <TEvent> > > RegisterAsync <TEvent>(this IEventDispatcher eventDispatcher, IEventHandler <TEvent> eventHandler) { if (eventDispatcher == null) { throw new ArgumentNullException(nameof(eventDispatcher)); } return(eventDispatcher.RegisterAsync(ContextualProvider.FromValue(eventHandler))); }
public async Task DeregisterTest() { var serviceProvider = BuildServiceProvider(); var messageDispatcher = new MessageDispatcher(serviceProvider); // Register a handler for the message type and deregister it to create a typed dispatcher var handler = new TestMessageHandler(); var registration = messageDispatcher.Register(ContextualProvider.Create(handler)); var baseHandler = new TestMessageBaseHandler(); messageDispatcher.Register(ContextualProvider.Create(baseHandler)); registration.Dispose(); await registration.Disposal; var message = new TestMessage("x", 123); var dispatchResult = await messageDispatcher.DispatchAsync(message, new DispatchValueDictionary(), publish : false, cancellation : default); Assert.IsTrue(dispatchResult.IsSuccess); Assert.IsNull(handler.Message); Assert.AreEqual(message, baseHandler.Message); }