private async Task OnConsumeEvent(EventWrapper wrapper, List <IEventHandler> handlers) { var @event = wrapper.Event; using (var scope = _serviceScopeFactory.CreateScope()) { try { var context = new LightweightEventContext() { EventBus = this, ServiceProvider = scope.ServiceProvider, RouteKey = wrapper.Route, RetryCount = wrapper.RetryCount }; if (OnConsuming != null) { await OnConsuming(@event, context); } foreach (var handle in handlers) { await handle.OnEvent(wrapper.Event, context); } if (OnConsumed != null) { await OnConsumed(@event, context); } } catch (Exception ex) { if (OnConsumeError == null) { throw; } var context = new LightweightEventErrorContext { EventBus = this, ServiceProvider = scope.ServiceProvider, RouteKey = wrapper.Route, RetryCount = wrapper.RetryCount, Exception = ex }; await OnConsumeError(@event, context); } } }
private async Task PublishAsync(string eventRoute, IEvent @event, int retryCount = 0) { using (var scope = _serviceScopeFactory.CreateScope()) { try { var context = new LightweightEventContext { ServiceProvider = scope.ServiceProvider, EventBus = this, RouteKey = eventRoute, RetryCount = retryCount }; if (OnProducing != null) { await OnProducing(@event, context); } long sequence = _ringBuffer.Next(); var wapper = _ringBuffer[sequence]; wapper.Route = eventRoute; wapper.Event = @event; wapper.RetryCount = retryCount; _ringBuffer.Publish(sequence); if (OnProduced != null) { await OnProduced(@event, context); } } catch (Exception ex) { if (OnProductError == null) { throw; } var context = new LightweightEventErrorContext { ServiceProvider = scope.ServiceProvider, EventBus = this, RouteKey = eventRoute, RetryCount = retryCount, Exception = ex }; await OnProductError(@event, context); } } }