public async Task <TResponse> RequestAsync <TResponse>(IRequest <TResponse> request) { var route = _messageRoutes.GetRoute(request.GetType()); if (!route.Actions.Any()) { return(default(TResponse)); } if (route.Actions.Length > 1) { throw new ArgumentException( $"The request '{route.MessageType.FullName}' gave more than one route. This does not make sense in a Request-Response scenario. Use Publish for that or inspect your registered handlers.", nameof(request)); } var action = route.Actions[0]; var envelope = new MessageEnvelope(request, route.MessageType); OnBeforeRouting?.Invoke(envelope); try { var handler = _messageHandlerCreator(action.HandlerType, envelope); var resultingTask = (Task <TResponse>)action.Invoke(handler, envelope.Message); return(await resultingTask.ConfigureAwait(false)); } finally { OnAfterRouted?.Invoke(envelope); } }
public async Task SendAsync(object message) { var route = _messageRoutes.GetRoute(message.GetType()); if (route.Actions.Length > 1) { throw new ArgumentException( $"The message '{route.MessageType.FullName}' gave more than one route. This does not make sense in a Send scenario. Use Publish for that or inspect your registered handlers.", nameof(message)); } var action = route.Actions[0]; var envelope = new MessageEnvelope(message, route.MessageType); OnBeforeRouting?.Invoke(envelope); try { var handler = _messageHandlerCreator(action.HandlerType, envelope); var resultingTask = (Task)action.Invoke(handler, envelope.Message); await resultingTask.ConfigureAwait(false); } finally { OnAfterRouted?.Invoke(envelope); } }
public async Task RouteAsync <T>(T message) { var route = _messageRoutes.GetRoute(message.GetType()); var envelope = new MessageEnvelope(message, route.MessageType); OnBeforeRouting?.Invoke(envelope); try { foreach (var action in route.Actions) { await action.Invoke(_messageHandlerCreator(action.HandlerType, envelope), envelope.Message).ConfigureAwait(false); } } finally { OnAfterRouted?.Invoke(envelope); } }
public void Publish(object message) { var route = _messageRoutes.GetRoute(message.GetType()); var envelope = new MessageEnvelope(message, route.MessageType); OnBeforeRouting?.Invoke(envelope); try { foreach (var action in route.Actions) { var handler = _messageHandlerCreator(action.HandlerType, envelope); action.Invoke(handler, envelope.Message); } } finally { OnAfterRouted?.Invoke(envelope); } }
public async Task PublishAsync(object message) { var route = _messageRoutes.GetRoute(message.GetType()); var envelope = new MessageEnvelope(message, route.MessageType); OnBeforeRouting?.Invoke(envelope); try { foreach (var action in route.Actions) { var handler = _messageHandlerCreator(action.HandlerType, envelope); var resultingTask = (Task)action.Invoke(handler, envelope.Message); await resultingTask.ConfigureAwait(false); } } finally { OnAfterRouted?.Invoke(envelope); } }