Exemplo n.º 1
0
        private async Task PublishAsync(string route, IEvent @event, int retryCount = 0)
        {
            using (var scope = _serviceScopeFactory.CreateScope())
            {
                try
                {
                    var context = new RedisEventContext
                    {
                        ServiceProvider = scope.ServiceProvider,
                        EventBus        = this,
                        RouteKey        = route,
                        RetryCount      = retryCount
                    };

                    if (OnProducing != null)
                    {
                        await OnProducing(@event, context);
                    }

                    await _redisClient.LPushAsync(_key, new EventWrapper
                    {
                        Route      = route,
                        Event      = @event.ToJsonString(),
                        RetryCount = retryCount
                    });

                    if (OnProduced != null)
                    {
                        await OnProduced(@event, context);
                    }
                }
                catch (Exception ex)
                {
                    if (OnProductError == null)
                    {
                        throw;
                    }

                    var context = new RedisEventErrorContext
                    {
                        ServiceProvider = scope.ServiceProvider,
                        EventBus        = this,
                        RouteKey        = route,
                        RetryCount      = retryCount,
                        Exception       = ex
                    };
                    await OnProductError(@event, context);
                }
            }
        }
Exemplo n.º 2
0
        private async Task OnConsumeEvent(EventWrapper wrapper, List <IEventHandler> handlers)
        {
            IEvent firstEvent = null;

            using (var scope = _serviceScopeFactory.CreateScope())
            {
                try
                {
                    var context = new RedisEventContext
                    {
                        Key             = _key,
                        EventBus        = this,
                        ServiceProvider = scope.ServiceProvider,
                        RouteKey        = wrapper.Route,
                    };
                    Type eventType;

                    var eventTypeDict = new Dictionary <Type, IEvent>();
                    for (var i = 0; i < handlers.Count; i++)
                    {
                        eventType = handlers[0].GetEventType();
                        IEvent @event;
                        if (!eventTypeDict.ContainsKey(eventType))
                        {
                            @event = wrapper.Event.ToObject(eventType) as IEvent;
                            eventTypeDict.Add(eventType, @event);
                        }
                        else
                        {
                            @event = eventTypeDict[eventType];
                        }

                        if (i == 0)
                        {
                            firstEvent = @event;
                            if (OnConsuming != null)
                            {
                                await OnConsuming(firstEvent, context);
                            }
                        }

                        await handlers[i].OnEvent(@event, context);
                    }

                    if (OnConsumed != null && firstEvent != null)
                    {
                        await OnConsumed(firstEvent, context);
                    }
                }
                catch (Exception ex)
                {
                    if (OnConsumeError == null || firstEvent == null)
                    {
                        throw;
                    }

                    var context = new RedisEventErrorContext
                    {
                        EventBus        = this,
                        ServiceProvider = scope.ServiceProvider,
                        RouteKey        = wrapper.Route,
                        RetryCount      = wrapper.RetryCount,
                        Exception       = ex
                    };
                    await OnConsumeError(firstEvent, context);
                }
            }
        }