protected override object CreateHandlerFromScope <TBusEvent>(IDependencyResolverScope scope, TBusEvent busEvent, Type handlerType, NimbusMessage nimbusMessage) { var handler = (IHandleCompetingEvent <TBusEvent>)scope.Resolve(handlerType); _propertyInjector.Inject(handler, nimbusMessage); return(handler); }
protected override object CreateHandlerFromScope <TBusEvent>(IDependencyResolverScope scope, TBusEvent busEvent, Type handlerType, BrokeredMessage brokeredMessage) { var handler = (IHandleMulticastEvent <TBusEvent>)scope.Resolve(handlerType); _propertyInjector.Inject(handler, brokeredMessage); return(handler); }
public IOutboundInterceptor[] CreateInterceptors(IDependencyResolverScope scope, BrokeredMessage brokeredMessage) { return(_globalOutboundInterceptorTypes .Value .Select(t => (IOutboundInterceptor)scope.Resolve(t)) .Do(interceptor => _propertyInjector.Inject(interceptor, brokeredMessage)) .ToArray()); }
public IInboundInterceptor[] CreateInterceptors(IDependencyResolverScope scope, object handler, object message, NimbusMessage nimbusMessage) { var globalInterceptors = GetGlobalInterceptorTypes(); var classLevelInterceptors = GetClassLevelInterceptorTypes(handler); var methodLevelInterceptors = GetMethodLevelInterceptorTypes(handler, message); var interceptors = new Type[0] .Union(globalInterceptors) .Union(classLevelInterceptors) .Union(methodLevelInterceptors) .DistinctBy(t => t.FullName) .Select(t => (IInboundInterceptor)scope.Resolve(t)) .Do(interceptor => _propertyInjector.Inject(interceptor, nimbusMessage)) .OrderByDescending(i => i.Priority) .ThenBy(i => i.GetType().FullName) .ToArray(); return(interceptors); }
// ReSharper disable UnusedMember.Local private async Task Dispatch <TBusRequest, TBusResponse>(TBusRequest busRequest, NimbusMessage nimbusMessage, Type handlerType) where TBusRequest : IBusMulticastRequest <TBusRequest, TBusResponse> where TBusResponse : IBusMulticastResponse { var replyQueueName = nimbusMessage.From; var replyQueueClient = _transport.GetQueueSender(replyQueueName); Exception exception = null; using (var scope = _dependencyResolver.CreateChildScope()) { var handler = (IHandleMulticastRequest <TBusRequest, TBusResponse>)scope.Resolve(handlerType); _propertyInjector.Inject(handler, nimbusMessage); var inboundInterceptors = _inboundInterceptorFactory.CreateInterceptors(scope, handler, busRequest, nimbusMessage); foreach (var interceptor in inboundInterceptors) { _logger.Debug("Executing OnRequestHandlerExecuting on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); await interceptor.OnMulticastRequestHandlerExecuting(busRequest, nimbusMessage); _logger.Debug("Executed OnRequestHandlerExecuting on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); } try { var response = await handler.Handle(busRequest); if (response != null) { var responseMessage = await _nimbusMessageFactory.CreateSuccessfulResponse(replyQueueName, response, nimbusMessage); DispatchLoggingContext.NimbusMessage = responseMessage; var outboundInterceptors = _outboundInterceptorFactory.CreateInterceptors(scope, nimbusMessage); _logger.Debug("Sending successful response message {0} to {1} [MessageId:{2}, CorrelationId:{3}]", responseMessage.SafelyGetBodyTypeNameOrDefault(), replyQueueName, nimbusMessage.MessageId, nimbusMessage.CorrelationId); foreach (var interceptor in outboundInterceptors) { await interceptor.OnMulticastResponseSending(response, nimbusMessage); } await replyQueueClient.Send(responseMessage); foreach (var interceptor in outboundInterceptors.Reverse()) { await interceptor.OnMulticastResponseSent(response, nimbusMessage); } _logger.Debug("Sent successful response message {0} to {1} [MessageId:{2}, CorrelationId:{3}]", nimbusMessage.SafelyGetBodyTypeNameOrDefault(), replyQueueName, nimbusMessage.MessageId, nimbusMessage.CorrelationId); } else { _logger.Debug("Handler declined to reply. [MessageId: {0}, CorrelationId: {1}]", nimbusMessage.MessageId, nimbusMessage.CorrelationId); } } catch (Exception exc) { // Capture any exception so we can send a failed response outside the catch block exception = exc; } if (exception == null) { foreach (var interceptor in inboundInterceptors.Reverse()) { _logger.Debug("Executing OnRequestHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); await interceptor.OnMulticastRequestHandlerSuccess(busRequest, nimbusMessage); _logger.Debug("Executed OnRequestHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); } } else { foreach (var interceptor in inboundInterceptors.Reverse()) { _logger.Debug("Executing OnRequestHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); await interceptor.OnMulticastRequestHandlerError(busRequest, nimbusMessage, exception); _logger.Debug("Executed OnRequestHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); } var failedResponseMessage = await _nimbusMessageFactory.CreateFailedResponse(replyQueueName, nimbusMessage, exception); _logger.Warn("Sending failed response message to {0} [MessageId:{1}, CorrelationId:{2}]", replyQueueName, exception.Message, nimbusMessage.MessageId, nimbusMessage.CorrelationId); await replyQueueClient.Send(failedResponseMessage); _logger.Debug("Sent failed response message to {0} [MessageId:{1}, CorrelationId:{2}]", replyQueueName, nimbusMessage.MessageId, nimbusMessage.CorrelationId); } } }
private async Task Dispatch <TBusCommand>(TBusCommand busCommand, BrokeredMessage brokeredMessage, Type handlerType) where TBusCommand : IBusCommand { using (var scope = _dependencyResolver.CreateChildScope()) { var handler = (IHandleCommand <TBusCommand>)scope.Resolve(handlerType); _propertyInjector.Inject(handler, brokeredMessage); var interceptors = _inboundInterceptorFactory.CreateInterceptors(scope, handler, busCommand, brokeredMessage); Exception exception; try { foreach (var interceptor in interceptors) { _logger.Debug( "Executing OnCommandHandlerExecuting on {InterceptorType} for message [MessageType:{MessageType}, MessageId:{MessageId}, CorrelationId:{CorrelationId}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); await interceptor.OnCommandHandlerExecuting(busCommand, brokeredMessage); _logger.Debug( "Executed OnCommandHandlerExecuting on {InterceptorType} for message [MessageType:{MessageType}, MessageId:{MessageId}, CorrelationId:{CorrelationId}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); } var handlerTask = _taskFactory.StartNew(async() => await handler.Handle(busCommand), TaskContext.Handle).Unwrap(); var longRunningTask = handler as ILongRunningTask; if (longRunningTask != null) { var wrapper = new LongRunningTaskWrapper(handlerTask, longRunningTask, brokeredMessage, _clock, _logger, _defaultMessageLockDuration, _taskFactory); await wrapper.AwaitCompletion(); } else { await handlerTask; } foreach (var interceptor in interceptors.Reverse()) { _logger.Debug("Executing OnCommandHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); await interceptor.OnCommandHandlerSuccess(busCommand, brokeredMessage); _logger.Debug("Executed OnCommandHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); } return; } catch (Exception exc) { exception = exc; } foreach (var interceptor in interceptors.Reverse()) { _logger.Debug("Executing OnCommandHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); await interceptor.OnCommandHandlerError(busCommand, brokeredMessage, exception); _logger.Debug("Executed OnCommandHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); } _logger.Debug("Failed to Dispatch CommandMessage for message [MessageType:{0}, MessageId:{1}, CorrelationId:{2}]", brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); throw new DispatchFailedException("Failed to Dispatch CommandMessage", exception); } }
// ReSharper disable UnusedMember.Local private async Task Dispatch <TBusRequest, TBusResponse>(TBusRequest busRequest, BrokeredMessage brokeredMessage, Type handlerType) where TBusRequest : IBusMulticastRequest <TBusRequest, TBusResponse> where TBusResponse : IBusMulticastResponse { var replyQueueName = brokeredMessage.ReplyTo; var replyQueueClient = _messagingFactory.GetQueueSender(replyQueueName); Exception exception = null; using (var scope = _dependencyResolver.CreateChildScope()) { var handler = (IHandleMulticastRequest <TBusRequest, TBusResponse>)scope.Resolve(handlerType); _propertyInjector.Inject(handler, brokeredMessage); var inboundInterceptors = _inboundInterceptorFactory.CreateInterceptors(scope, handler, busRequest, brokeredMessage); foreach (var interceptor in inboundInterceptors) { _logger.Debug("Executing OnRequestHandlerExecuting on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); await interceptor.OnMulticastRequestHandlerExecuting(busRequest, brokeredMessage); _logger.Debug("Executed OnRequestHandlerExecuting on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); } try { var handlerTask = handler.Handle(busRequest); var longRunningTask = handlerTask as ILongRunningTask; TBusResponse response; if (longRunningTask != null) { var wrapperTask = new LongRunningTaskWrapper <TBusResponse>(handlerTask, longRunningTask, brokeredMessage, _clock, _logger, _defaultMessageLockDuration, _taskFactory); response = await wrapperTask.AwaitCompletion(); } else { response = await handlerTask; } // ReSharper disable CompareNonConstrainedGenericWithNull if (response != null) // ReSharper restore CompareNonConstrainedGenericWithNull { var responseMessage = (await _brokeredMessageFactory.CreateSuccessfulResponse(response, brokeredMessage)) .DestinedForQueue(replyQueueName) ; var outboundInterceptors = _outboundInterceptorFactory.CreateInterceptors(scope, brokeredMessage); _logger.Debug("Sending successful response message {0} to {1} [MessageId:{2}, CorrelationId:{3}]", responseMessage.SafelyGetBodyTypeNameOrDefault(), replyQueueName, brokeredMessage.MessageId, brokeredMessage.CorrelationId); foreach (var interceptor in outboundInterceptors) { await interceptor.OnMulticastResponseSending(response, brokeredMessage); } await replyQueueClient.Send(responseMessage); foreach (var interceptor in outboundInterceptors.Reverse()) { await interceptor.OnMulticastResponseSent(response, brokeredMessage); } _logger.Info("Sent successful response message {0} to {1} [MessageId:{2}, CorrelationId:{3}]", brokeredMessage.SafelyGetBodyTypeNameOrDefault(), replyQueueName, brokeredMessage.MessageId, brokeredMessage.CorrelationId); } else { _logger.Info("Handler declined to reply. [MessageId: {0}, CorrelationId: {1}]", brokeredMessage.MessageId, brokeredMessage.CorrelationId); } } catch (Exception exc) { // Capture any exception so we can send a failed response outside the catch block exception = exc; } if (exception == null) { foreach (var interceptor in inboundInterceptors.Reverse()) { _logger.Debug("Executing OnRequestHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); await interceptor.OnMulticastRequestHandlerSuccess(busRequest, brokeredMessage); _logger.Debug("Executed OnRequestHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); } } else { foreach (var interceptor in inboundInterceptors.Reverse()) { _logger.Debug("Executing OnRequestHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); await interceptor.OnMulticastRequestHandlerError(busRequest, brokeredMessage, exception); _logger.Debug("Executed OnRequestHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, brokeredMessage.SafelyGetBodyTypeNameOrDefault(), brokeredMessage.MessageId, brokeredMessage.CorrelationId); } var failedResponseMessage = await _brokeredMessageFactory.CreateFailedResponse(brokeredMessage, exception); _logger.Warn("Sending failed response message to {0} [MessageId:{1}, CorrelationId:{2}]", replyQueueName, exception.Message, brokeredMessage.MessageId, brokeredMessage.CorrelationId); await replyQueueClient.Send(failedResponseMessage); _logger.Info("Sent failed response message to {0} [MessageId:{1}, CorrelationId:{2}]", replyQueueName, brokeredMessage.MessageId, brokeredMessage.CorrelationId); } } }
// ReSharper disable UnusedMember.Local private async Task Dispatch <TBusRequest, TBusResponse>(TBusRequest busRequest, NimbusMessage nimbusMessage, Type handlerType) where TBusRequest : IBusRequest <TBusRequest, TBusResponse> where TBusResponse : IBusResponse { var replyQueueName = nimbusMessage.From; var replyQueueClient = _transport.GetQueueSender(replyQueueName); using (var scope = _dependencyResolver.CreateChildScope()) { var handler = (IHandleRequest <TBusRequest, TBusResponse>)scope.Resolve(handlerType); _propertyInjector.Inject(handler, nimbusMessage); var inboundInterceptors = _inboundInterceptorFactory.CreateInterceptors(scope, handler, busRequest, nimbusMessage); Exception exception = null; try { foreach (var interceptor in inboundInterceptors) { _logger.Debug("Executing OnRequestHandlerExecuting for {InterceptorType}", interceptor.GetType().Name); await interceptor.OnRequestHandlerExecuting(busRequest, nimbusMessage); _logger.Debug("Executed OnRequestHandlerExecuting for {InterceptorType}", interceptor.GetType().Name); } _logger.Debug("Dispatching to {HandlerType}", handler.GetType().Name); var handlerTask = handler.Handle(busRequest); var response = await handlerTask; _logger.Debug("Dispatched to {HandlerType}", handler.GetType().Name); var responseMessage = await _nimbusMessageFactory.CreateSuccessfulResponse(replyQueueName, response, nimbusMessage); DispatchLoggingContext.NimbusMessage = responseMessage; var outboundInterceptors = _outboundInterceptorFactory.CreateInterceptors(scope, nimbusMessage); foreach (var interceptor in outboundInterceptors.Reverse()) { _logger.Debug("Executing OnResponseSending for {InterceptorType}", interceptor.GetType().Name); await interceptor.OnResponseSending(response, responseMessage); _logger.Debug("Executed OnResponseSending for {InterceptorType}", interceptor.GetType().Name); } _logger.Debug("Sending successful response message"); await replyQueueClient.Send(responseMessage); _logger.Debug("Sent successful response message"); foreach (var interceptor in outboundInterceptors.Reverse()) { _logger.Debug("Executing OnResponseSent for {InterceptorType}", interceptor.GetType().Name); await interceptor.OnResponseSent(response, responseMessage); _logger.Debug("Executed OnResponseSent for {InterceptorType}", interceptor.GetType().Name); } } catch (Exception exc) { // Capture any exception so we can send a failed response outside the catch block exception = exc; _logger.Warn(exc, "Request message dispatch failed."); } if (exception == null) { foreach (var interceptor in inboundInterceptors.Reverse()) { _logger.Debug("Executing OnRequestHandlerSuccess for {InterceptorType}", interceptor.GetType().Name); await interceptor.OnRequestHandlerSuccess(busRequest, nimbusMessage); _logger.Debug("Executed OnRequestHandlerSuccess for {InterceptorType}", interceptor.GetType().Name); } } else { foreach (var interceptor in inboundInterceptors.Reverse()) { _logger.Debug("Executing OnRequestHandlerError for {InterceptorType}", interceptor.GetType().Name); await interceptor.OnRequestHandlerError(busRequest, nimbusMessage, exception); _logger.Debug("Executed OnRequestHandlerError for {InterceptorType}", interceptor.GetType().Name); } var failedResponseMessage = (await _nimbusMessageFactory.CreateFailedResponse(replyQueueName, nimbusMessage, exception)); _logger.Debug("Sending failed response message"); await replyQueueClient.Send(failedResponseMessage); _logger.Debug("Sent failed response message"); } } }
private async Task Dispatch <TBusCommand>(TBusCommand busCommand, NimbusMessage nimbusMessage, Type handlerType) where TBusCommand : IBusCommand { using (var scope = _dependencyResolver.CreateChildScope()) { var handler = (IHandleCommand <TBusCommand>)scope.Resolve(handlerType); _propertyInjector.Inject(handler, nimbusMessage); var interceptors = _inboundInterceptorFactory.CreateInterceptors(scope, handler, busCommand, nimbusMessage); Exception exception; try { foreach (var interceptor in interceptors) { _logger.Debug( "Executing OnCommandHandlerExecuting on {InterceptorType} for message [MessageType:{MessageType}, MessageId:{MessageId}, CorrelationId:{CorrelationId}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); await interceptor.OnCommandHandlerExecuting(busCommand, nimbusMessage); _logger.Debug( "Executed OnCommandHandlerExecuting on {InterceptorType} for message [MessageType:{MessageType}, MessageId:{MessageId}, CorrelationId:{CorrelationId}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); } await handler.Handle(busCommand); foreach (var interceptor in interceptors.Reverse()) { _logger.Debug("Executing OnCommandHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); await interceptor.OnCommandHandlerSuccess(busCommand, nimbusMessage); _logger.Debug("Executed OnCommandHandlerSuccess on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); } return; } catch (Exception exc) { exception = exc; } foreach (var interceptor in interceptors.Reverse()) { _logger.Debug("Executing OnCommandHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); await interceptor.OnCommandHandlerError(busCommand, nimbusMessage, exception); _logger.Debug("Executed OnCommandHandlerError on {0} for message [MessageType:{1}, MessageId:{2}, CorrelationId:{3}]", interceptor.GetType().FullName, nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); } _logger.Debug("Failed to Dispatch CommandMessage for message [MessageType:{0}, MessageId:{1}, CorrelationId:{2}]", nimbusMessage.SafelyGetBodyTypeNameOrDefault(), nimbusMessage.MessageId, nimbusMessage.CorrelationId); throw new DispatchFailedException("Failed to Dispatch CommandMessage", exception); } }