/// <summary> /// Perform a request/response /// </summary> /// <typeparam name="TMessage">The type of the message being sent</typeparam> /// <typeparam name="TEvent">The type of the event for the response</typeparam> /// <param name="bus">The bus to send/receive from</param> /// <param name="message">The message to send</param> /// <param name="cancellationToken">CancellationToken to use to cancel or timeout.</param> /// <returns>The event response</returns> public static Task <TEvent> RequestAsync <TMessage, TEvent>(this IBus bus, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage where TEvent : IEvent { if (bus == null) { throw new ArgumentNullException(nameof(bus)); } if (message == null) { throw new ArgumentNullException(nameof(message)); } var tcs = new TaskCompletionSource <TEvent>(); cancellationToken.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false); ActionConsumer <TEvent> actionConsumer = null; actionConsumer = new ActionConsumer <TEvent>(e => { if (actionConsumer != null) { bus.RemoveHandler(actionConsumer); } tcs.SetResult(e); }); bus.AddHandler(actionConsumer); bus.Send(message); return(tcs.Task); }
public Messaging() { bus = new Bus(); bus.AddHandler(new ActionConsumer <IMessage>(message => { messages.Add(message); })); }
public void SetUp() { bus = new TestBus(); messages = new List <IMessage>(); bus.AddHandler(new ActionConsumer <IMessage>(message => { messages.Add(message); })); }
public void AddHandler(IBus bus, IConsumer <TMessage> consumer) { bus.AddHandler(consumer); }
/// <summary> /// Perform a request/response /// </summary> /// <typeparam name="TMessage">The type of the message being sent</typeparam> /// <typeparam name="TSuccessEvent">The type of the event for the response</typeparam> /// <param name="bus">The bus to send/receive from</param> /// <param name="message">The message to send</param> /// <param name="cancellationToken">CancellationToken to use to cancel or timeout.</param> /// <returns>The event response</returns> public static Task <TSuccessEvent> RequestAsync <TMessage, TSuccessEvent, TErrorEvent>(this IBus bus, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage where TSuccessEvent : IEvent where TErrorEvent : IEvent { if (bus == null) { throw new ArgumentNullException(nameof(bus)); } if (message == null) { throw new ArgumentNullException(nameof(message)); } var tcs = new TaskCompletionSource <TSuccessEvent>(); ActionConsumer <TSuccessEvent> successActionConsumer = null; ActionConsumer <TErrorEvent> errorActionConsumer = null; object successToken = null; object errorToken = null; cancellationToken.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false); { successActionConsumer = new ActionConsumer <TSuccessEvent>(e => { try { if (e.CorrelationId != message.CorrelationId) { return; } if (successActionConsumer != null) { bus.RemoveHandler(successActionConsumer, successToken); } if (errorActionConsumer != null) { bus.RemoveHandler(errorActionConsumer, errorToken); } tcs.SetResult(e); } catch (Exception ex) { tcs.SetException(ex); } }); successToken = bus.AddHandler(successActionConsumer); } { errorActionConsumer = new ActionConsumer <TErrorEvent>(e => { try { if (e.CorrelationId != message.CorrelationId) { return; } if (errorActionConsumer != null) { bus.RemoveHandler(errorActionConsumer, errorToken); } if (successActionConsumer != null) { bus.RemoveHandler(successActionConsumer, successToken); } tcs.SetException(new ReceivedErrorEventException <TErrorEvent>(e)); } catch (Exception ex) { tcs.SetException(ex); } }); errorToken = bus.AddHandler(errorActionConsumer); } bus.Send(message); return(tcs.Task); }