Esempio n. 1
0
        /// <summary>
        /// Calls the fault handler on the message object
        /// </summary>
        /// <param name="message">Message to process</param>
        /// <param name="busInstance">Instance of the bus to pass to event handlers</param>
        /// <param name="messageException">Exception caused by the message</param>
        private void ProcessFaultedMessageHandlers(object message, IBus busInstance, Exception messageException)
        {
            Type messageType = message.GetType();

            if (_faultHandlers.ContainsKey(messageType))
            {
                Log.TraceFormat("Processing {0} fault handlers for message type {1}", _faultHandlers[messageType].Count, messageType.FullName);
                var handlerMethod = typeof(IHandleMessageFaults <>).MakeGenericType(messageType).GetMethod("Handle");
                foreach (Type handlerType in _faultHandlers[messageType])
                {
                    using (IObjectBuilder childBuilder = _objectBuilder.GetNestedBuilder())
                    {
                        if (busInstance != null)
                        {
                            childBuilder.RegisterInstance <IBus>(busInstance);
                        }

                        childBuilder.RegisterInstance(LogManager.GetLogger(handlerType));
                        Log.TraceFormat("Running handler {0}", handlerType.Name);
                        try
                        {
                            var handler = childBuilder.GetValue(handlerType);
                            handlerMethod.Invoke(handler, new object[] { message, messageException });
                        }
                        catch (Exception ex)
                        {
                            Log.ErrorFormat("Error in handling message fault {0} with {1}", ex, messageType.Name, handlerType.Name);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Process the given message
        /// </summary>
        /// <param name="message">Transport message to be dispatched to the event handlers</param>
        /// <param name="busInstance">Instance of the send bus to inject into the event handlers</param>
        /// <returns>True is all event handlers processed successfully</returns>
        public MessageProcessingResult ProcessMessage(TransportMessage message, IBus busInstance)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            MessageProcessingResult result = new MessageProcessingResult();
            Stopwatch watch = new Stopwatch();

            watch.Start();
            if (_handlerTypes.ContainsKey(message.MessageType))
            {
                Log.TraceFormat("Processing {0} handlers for message type {1}", _handlerTypes[message.MessageType].Count, message.MessageTypeName);
                var handlerMethod = typeof(IHandleMessage <>).MakeGenericType(message.MessageType).GetMethod("Handle");
                using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
                {
                    foreach (Type handlerType in _handlerTypes[message.MessageType])
                    {
                        using (IObjectBuilder childBuilder = _objectBuilder.GetNestedBuilder())
                        {
                            if (busInstance != null)
                            {
                                childBuilder.RegisterInstance <IBus>(busInstance);
                            }

                            childBuilder.RegisterInstance(LogManager.GetLogger(handlerType));
                            Log.TraceFormat("Running handler {0}", handlerType.Name);
                            try
                            {
                                var handler = childBuilder.GetValue(handlerType);
                                handlerMethod.Invoke(handler, new object[] { message.Message });
                            }
                            catch (TargetInvocationException ex)
                            {
                                Log.ErrorFormat("Error in handling {0} with {1}", ex, message.MessageType.Name, handlerType.Name);
                                result.Exception = ex.InnerException ?? ex;
                            }
                            catch (Exception ex)
                            {
                                Log.ErrorFormat("Error in handling {0} with {1}", ex, message.MessageType.Name, handlerType.Name);
                                result.Exception = ex;
                            }
                        }
                    }

                    result.WasSuccessful = result.Exception == null;
                    if (result.WasSuccessful)
                    {
                        transactionScope.Complete();
                    }
                }
            }
            else
            {
                Log.WarnFormat("Could not find a handler for {0}", message.MessageTypeName);
                result.Exception = new Exception(string.Format("Could not find a handler for {0}", message.MessageTypeName));
            }

            watch.Stop();
            result.Runtime = watch.Elapsed;

            return(result);
        }