コード例 #1
0
 private async Task TryReportError <TEvent>(Exception exception, IEventHandlerAsync <TEvent> handler, TEvent @event)
 {
     try
     {
         await handler.OnErrorAsync(exception, @event).ConfigureAwait(false);
     }
     catch
     {
         // yes, we mute exceptions here
     }
 }
コード例 #2
0
        private async Task TryRunHandler <TEvent>(IEventHandlerAsync <TEvent> handler, TEvent @event)
        {
            try
            {
                if (!await handler.ShouldHandleAsync(@event).ConfigureAwait(false))
                {
                    return;
                }

                await handler.HandleAsync(@event).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                await TryReportError(exception, handler, @event).ConfigureAwait(false);
            }
        }
コード例 #3
0
 /// <summary>
 /// Removes subscription for events of type <typeparamref name="TEvent"/>.
 /// </summary>
 /// <typeparam name="TEvent">The type of the event.</typeparam>
 /// <param name="handler">An instance of IEventHandler&lt;TEvent&gt; to remove form subscribers.</param>
 public void Unsubscribe <TEvent>(IEventHandlerAsync <TEvent> handler)
 {
     Unsubscribe <TEvent>(x => x.IsWrapping(handler));
 }
コード例 #4
0
        /// <summary>
        /// Adds subscription for events of type <typeparamref name="TEvent"/>.
        /// </summary>
        /// <typeparam name="TEvent">The type of the event.</typeparam>
        /// <param name="handler">An instance of IEventHandler&lt;TEvent&gt; which Handle method will be invoked when event is published.</param>
        public void Subscribe <TEvent>(IEventHandlerAsync <TEvent> handler)
        {
            var handlers = _subscribers.GetOrAdd(typeof(TEvent), _ => new List <object>());

            handlers.Add(new EventHandlerAsyncWrapper <TEvent>(handler));
        }
コード例 #5
0
 public bool IsWrapping(IEventHandlerAsync <TEvent> eventHandler) => _eventHandler == eventHandler;
コード例 #6
0
 public EventHandlerAsyncWrapper(Func <TEvent, Task> handler, Func <TEvent, Task <bool> > filter = null, Func <Exception, TEvent, Task> onError = null)
 {
     _eventHandler = new DelegateEventHandlerAsync <TEvent>(handler, filter, onError);
     _handler      = handler;
     IsSubscribed  = true;
 }
コード例 #7
0
 public EventHandlerAsyncWrapper(IEventHandlerAsync <TEvent> eventHandler)
 {
     _eventHandler = eventHandler;
     _handler      = eventHandler.HandleAsync;
     IsSubscribed  = true;
 }
コード例 #8
0
 public static ISubscription WithEventHandlerAsync <TEvent>(this IBusClient bus,
                                                            IEventHandlerAsync <TEvent> handler, string exchangeName = null, string routingKey = null) where TEvent : IEvent
 => bus.SubscribeAsync <TEvent>(async(msg, context) => await handler.HandleAsync(msg),
                                cfg => cfg.WithExchange(e => e.WithName(exchangeName)).WithRoutingKey(routingKey));