Example #1
0
        private async ValueTask ProcessMessageHandlingExceptionAsync(IMessageScope scope, IMessageHandlerFactory asyncHandlerFactory, Type messageType, IMessage message, List <Exception> exceptions)
        {
            var asyncEventHandler = asyncHandlerFactory.GetHandler(scope);

            try
            {
                if (asyncEventHandler == null)
                {
                    throw new ArgumentNullException($"Registered async message handler for message type {messageType.Name} is null!");
                }

                var asyncHandlerType = typeof(IAsyncMessageHandler <>).MakeGenericType(messageType);

                var method = asyncHandlerType.GetMethod(
                    nameof(IAsyncMessageHandler <IMessage> .HandleMessageAsync),
                    new[] { messageType }
                    );

                await(ValueTask) method.Invoke(asyncEventHandler, new object[] { message });
            }
            catch (TargetInvocationException ex)
            {
                exceptions.Add(ex.InnerException);
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
            finally
            {
                asyncHandlerFactory.ReleaseHandler(scope, asyncEventHandler);
            }
        }
Example #2
0
        private async ValueTask ProcessRichMessageHandlingExceptionAsync(IMessageScope scope, IMessageHandlerFactory asyncHandlerFactory, Type messageType, IMessage message, IRichMessageDescriptor descriptor, List <Exception> exceptions)
        {
            var handlerInstance = asyncHandlerFactory.GetHandler(scope);

            try
            {
                if (handlerInstance == null)
                {
                    throw new ArgumentNullException($"Registered async rich message handler for message type {messageType.Name} is null!");
                }

                var ifType = typeof(IAsyncRichMessageHandler <>).MakeGenericType(messageType);

                var method = ifType.GetMethod(
                    nameof(IAsyncRichMessageHandler <IMessage> .HandleMessageAsync),
                    new[] { messageType, typeof(IRichMessageDescriptor) }
                    );



                await(ValueTask) method.Invoke(handlerInstance, new object[] { message, descriptor });
            }
            catch (TargetInvocationException ex)
            {
                exceptions.Add(ex.InnerException);
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
            finally
            {
                asyncHandlerFactory.ReleaseHandler(scope, handlerInstance);
            }
        }
Example #3
0
        private void ProcessMessageHandlingException(IMessageScope scope, IMessageHandlerFactory handlerFactory, Type messageType, IMessage message, List <Exception> exceptions)
        {
            var eventHandler = handlerFactory.GetHandler(scope);

            try
            {
                if (eventHandler == null)
                {
                    throw new ArgumentNullException($"Registered message handler for message type {messageType.Name} is null!");
                }

                var handlerType = typeof(IMessageHandler <>).MakeGenericType(messageType);

                var method = handlerType.GetMethod(
                    nameof(IMessageHandler <IMessage> .HandleMessage),
                    new[] { messageType }
                    );

                method.Invoke(eventHandler, new object[] { message });
            }
            catch (TargetInvocationException ex)
            {
                exceptions.Add(ex.InnerException);
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
            finally
            {
                handlerFactory.ReleaseHandler(scope, eventHandler);
            }
        }
        internal string Respond(Stream requestBody, ConfigureModel persistable, string provider)
        {
            var requestXml = new StreamReader(requestBody).ReadToEnd();

            //Hack to Log Xml as Exception.
            new LogEvent(requestXml).Raise();
            Console.WriteLine(requestXml);
            return(_messageHandlerFactory.GetHandler(requestXml, provider).HandleResponse(requestXml, persistable));
        }
Example #5
0
 private async Task HandleNextMessageAsync(string messageEnvelopeAsString)
 {
     try
     {
         var payload = JsonConvert.DeserializeObject <MessageBrokerEnvelope <NewOrderDto> >(messageEnvelopeAsString);
         var handler = _messageHandlerFactory.GetHandler(MessageTypes.NewOrder);
         await handler.ExecuteAsync(payload);
     }
     catch (Exception ex)
     {
         throw new Exception("Message processing failed.", ex);
     }
 }
Example #6
0
        private IMessageHandler GetHandler(IMessage message)
        {
            Contract.Requires(message != null);
            Contract.Ensures(Contract.Result <IMessageHandler>() != null);

            try
            {
                // get handler from factory
                return(_handlerFactory.GetHandler(message.GetType()));
            }
            catch (Exception e)
            {
                // rethrow exception has a message handling exception
                throw new MessageHandlingException(message, e, "Could not get a message handler for message.");
            }
        }
Example #7
0
        public async ValueTask <bool> CallAsync(IMessageScope scope, IMessageHandlerFactory handlerFactory, IMessage message, IRichMessageDescriptor messageDescriptor, List <Exception> exceptions = null, CancellationToken cancellationToken = default)
        {
            var             handlerDescriptor = handlerFactory.GetHandlerDescriptor();
            IMessageHandler handler           = null;

            try
            {
                handler = handlerFactory.GetHandler(scope);

                if (handlerDescriptor.IsAsync)
                {
                    await GetOrCreateAsyncHandlerCallCache(handlerDescriptor).Invoke(handler, message, messageDescriptor);
                }
                else
                {
                    GetOrCreateSyncHandlerCallCache(handlerDescriptor).Invoke(handler, message, messageDescriptor);
                }
            }
            catch (TargetInvocationException e)
            {
                exceptions?.Add(e.InnerException);
                return(false);
            }
            catch (Exception e)
            {
                exceptions?.Add(e);
                return(false);
            }
            finally
            {
                if (handler != null)
                {
                    handlerFactory.ReleaseHandler(scope, handler);
                }
            }
            return(true);
        }
Example #8
0
    public void HandleMessage(IQueueMessage message)
    {
        var handler = _messageHandlerFactory.GetHandler(message.MessageType);

        handler.HandleMessage(message);
    }
Example #9
0
 public void CanGetHandlerForRegisteredMessage()
 {
     Assert.IsNotNull(_factory.GetHandler(typeof(Message1)));
 }