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 } }
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); } }
/// <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<TEvent> to remove form subscribers.</param> public void Unsubscribe <TEvent>(IEventHandlerAsync <TEvent> handler) { Unsubscribe <TEvent>(x => x.IsWrapping(handler)); }
/// <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<TEvent> 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)); }
public bool IsWrapping(IEventHandlerAsync <TEvent> eventHandler) => _eventHandler == eventHandler;
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; }
public EventHandlerAsyncWrapper(IEventHandlerAsync <TEvent> eventHandler) { _eventHandler = eventHandler; _handler = eventHandler.HandleAsync; IsSubscribed = true; }
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));