private void MessageReceived(object sender, OutputGatewayEventHandlerArgs <TMessage, MessageHeader> args) { var listTask = new List <Task>(); Logger.Debug(string.Format("Ha llegado el Mensaje:{0}", args.SerializedMessage)); Logger.Debug(string.Format("Tipo del Mensaje:{0}", args.Header.BodyType)); //Buscar en los handlers para procesar el mensaje foreach (var type in _handlerRepository.GetHandlersByMessage(args.Message.GetType())) { var typeClosure = type; listTask.Add(Task.Factory.StartNew(() => { try { using (var messageContext = ContextManager.Instance.CreateNewContext()) { Logger.Debug(string.Format("Se abre el handler {0}: ", typeClosure.FullName)); InitializeContext(messageContext, args); using (var messageHandler = (IDisposable)messageContext.Resolve(typeClosure)) { InitializeMessageHandler(messageHandler); try { InvokeMethodHandle(messageHandler, args.Message); } catch (Exception exception) { Logger.Fatal("Error On Handler", exception); Logger.Fatal(string.Format("Message Error: {0}", args.SerializedMessage), exception); } } } } catch (Exception ex) { Logger.Error("Error Message Received", ex); Logger.Fatal(string.Format("Message Error: {0}", args.SerializedMessage), ex); } })); } Task.WaitAll(listTask.ToArray()); }
/// <summary> /// Messages the received. /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The args.</param> private void MessageReceived(object sender, OutputGatewayEventHandlerArgs <IControlMessage, MessageHeader> args) { var listTask = new List <Task>(); Logger.Debug("Control Message Received"); //Buscar en los handlers para procesar el mensaje foreach (Type type in _handlerRepository.GetHandlersByMessage(args.Message.GetType())) { Type typeClosure = type; listTask.Add(Task.Factory.StartNew(() => { try { using (IMessageContext messageContext = ContextManager.Instance.CreateNewContext()) { object messageHandler = messageContext.Resolve(typeClosure); SetPropertyToHandler(messageHandler, "Controller", this); SetPropertyToHandler(messageHandler, "Processor", Processor); InvokeMethodHandle(messageHandler, args.Message); } } catch (Exception ex) { var message = args.Message.ToJson(); Logger.Error(string.Format("Error Mensaje de Control: {0}", message), ex); InvokeOnErrorHandler(message, args.Header, args.SerializedMessage, typeClosure, ex); } })); } Task.WaitAll(listTask.ToArray()); }