public async Task Dispatch(TransportLevelMessage message)
        {
            var registration = GetMessageRegistrationFor(message);

            var unitOfWork = _unitOfWorkFactory.CreateForHandlerType(registration.HandlerInstanceType);

            if (unitOfWork == null)
            {
                throw new UnableToResolveUnitOfWorkForHandlerException($"Error! Unable to create unit of work for handler type \"{registration.HandlerInstanceType.FullName}\".");
            }

            var messageInstance = message.ReadDataAs(registration.MessageInstanceType);
            var context         = new MessageHandlerContext(message.Metadata);

            await unitOfWork.Run(async handler =>
            {
                if (handler == null)
                {
                    throw new InvalidMessageHandlerException($"Error! Message handler of type \"{registration.HandlerInstanceType.FullName}\" not instantiated in unit of work and message instance type of \"{registration.MessageInstanceType}\" for message type \"{registration.MessageType}\" can therefor not be handled.");
                }

                // TODO -- verify that the handler is in fact an implementation of IMessageHandler<registration.MessageInstanceType> to provider sane error messages.

                await ExecuteHandler((dynamic)messageInstance, (dynamic)handler, context);
            });
        }
        private MessageRegistration GetMessageRegistrationFor(TransportLevelMessage message)
        {
            var messageId    = message.Metadata.MessageId;
            var messageType  = message.Metadata.Type;
            var registration = _messageHandlerRegistry.GetRegistrationFor(messageType);

            if (registration == null)
            {
                throw new MissingMessageHandlerRegistrationException($"Error! A handler has not been registered for messages of type \"{messageType}\". Message with id \"{messageId}\" was not handled.");
            }

            return(registration);
        }
 private MessageRegistration GetMessageRegistrationFor(TransportLevelMessage message) =>
 _messageHandlerRegistry.GetRegistrationFor(message.Metadata.Type)
 ?? _fallbackHandler.GetFallback(message.Metadata.Type);
Exemple #4
0
 public MessageResult(TransportLevelMessage message, Func <Task> onCommit = null)
 {
     Message   = message;
     _onCommit = onCommit ?? EmptyCommitAction;
 }