public MessageHandlerInvokeResult Invoke(IPipelineEvent pipelineEvent) { Guard.AgainstNull(pipelineEvent, "pipelineEvent"); var state = pipelineEvent.Pipeline.State; var message = state.GetMessage(); var handler = GetHandler(message.GetType()); if (handler == null) { return(MessageHandlerInvokeResult.InvokeFailure()); } var transportMessage = state.GetTransportMessage(); var messageType = message.GetType(); ContextMethod contextMethod; lock (_lockInvoke) { if (!_cache.ContainsKey(messageType)) { var interfaceType = typeof(IMessageHandler <>).MakeGenericType(messageType); var method = handler.GetType().GetInterfaceMap(interfaceType).TargetMethods.SingleOrDefault(); if (method == null) { throw new ProcessMessageMethodMissingException(string.Format( EsbResources.ProcessMessageMethodMissingException, handler.GetType().FullName, messageType.FullName)); } _cache.Add(messageType, new ContextMethod { ContextType = typeof(HandlerContext <>).MakeGenericType(messageType), Method = handler.GetType().GetInterfaceMap(typeof(IMessageHandler <>).MakeGenericType(messageType)).TargetMethods.SingleOrDefault() }); } contextMethod = _cache[messageType]; } var handlerContext = Activator.CreateInstance(contextMethod.ContextType, _configuration, _transportMessageFactory, _pipelineFactory, _subscriptionManager, transportMessage, message, state.GetActiveState()); contextMethod.Method.Invoke(handler, new[] { handlerContext }); return(MessageHandlerInvokeResult.InvokedHandler(handler)); }
public MessageHandlerInvokeResult Invoke(IPipelineEvent pipelineEvent) { Guard.AgainstNull(pipelineEvent, "pipelineEvent"); var state = pipelineEvent.Pipeline.State; var bus = state.GetServiceBus(); var message = state.GetMessage(); var handler = bus.Configuration.MessageHandlerFactory.GetHandler(message); if (handler == null) { return(MessageHandlerInvokeResult.InvokeFailure()); } try { var transportMessage = state.GetTransportMessage(); var messageType = message.GetType(); var contextType = typeof(HandlerContext <>).MakeGenericType(messageType); var method = handler.GetType().GetMethod("ProcessMessage", new[] { contextType }); if (method == null) { throw new ProcessMessageMethodMissingException(string.Format( EsbResources.ProcessMessageMethodMissingException, handler.GetType().FullName, messageType.FullName)); } var handlerContext = Activator.CreateInstance(contextType, bus, transportMessage, message, state.GetActiveState()); method.Invoke(handler, new[] { handlerContext }); } finally { bus.Configuration.MessageHandlerFactory.ReleaseHandler(handler); } return(MessageHandlerInvokeResult.InvokedHandler(handler)); }
public MessageHandlerInvokeResult Invoke(IPipelineEvent pipelineEvent) { Guard.AgainstNull(pipelineEvent, nameof(pipelineEvent)); var state = pipelineEvent.Pipeline.State; var message = state.GetMessage(); var messageType = message.GetType(); var handler = GetHandler(messageType); if (handler == null) { return(MessageHandlerInvokeResult.InvokeFailure()); } var transportMessage = state.GetTransportMessage(); try { ContextMethod contextMethod; lock (LockInvoke) { if (!_cache.TryGetValue(messageType, out contextMethod)) { var interfaceType = typeof(IMessageHandler <>).MakeGenericType(messageType); var method = handler.GetType().GetInterfaceMap(interfaceType).TargetMethods.SingleOrDefault(); if (method == null) { throw new HandlerMessageMethodMissingException(string.Format( Resources.HandlerMessageMethodMissingException, handler.GetType().FullName, messageType.FullName)); } contextMethod = new ContextMethod { ContextType = typeof(HandlerContext <>).MakeGenericType(messageType), Method = handler.GetType() .GetInterfaceMap(typeof(IMessageHandler <>).MakeGenericType(messageType)) .TargetMethods.SingleOrDefault() }; _cache.Add(messageType, contextMethod); } } var handlerContext = Activator.CreateInstance(contextMethod.ContextType, _configuration, _transportMessageFactory, _pipelineFactory, _subscriptionManager, transportMessage, message, state.GetActiveState()); state.SetHandlerContext((IMessageSender)handlerContext); contextMethod.Method.Invoke(handler, new[] { handlerContext }); } finally { if (handler is IReusability reusability && !reusability.IsReusable) { ReleaseHandler(messageType); } } return(MessageHandlerInvokeResult.InvokedHandler(handler)); }