Пример #1
0
        public virtual async Task ProcessEvent(IDomainEvent e)
        {
            Logger.LogInformation($"(Read Model [{GetPrettyName()}] received event {e.GetPrettyName()}.");
            var applyMethods = GetEventApplyMethods(e);

            await applyMethods(e);

            Logger.LogInformation($"(Read Model [{GetPrettyName()}] handled event {e.GetPrettyName()}.");
        }
Пример #2
0
        public virtual async Task ProcessEvent(IDomainEvent e)
        {
            Logger.LogInformation("(Saga [{PrettyName}] received event {EventPrettyName}", GetPrettyName(), e.GetPrettyName());

            //Проверяем, является ли он стартовым
            if (!StartedEventTypes.Contains(e.EventType) && !_isStarted)
            {
                Logger.LogWarning($"(Saga [{GetPrettyName()}] event {e.GetPrettyName()} is not start-event.");
                return; //Игнорируем
            }

            _pinnedEvent = e;

            //Запускаем транзакцию
            await State.BeginTransaction();

            try
            {
                var applyMethod = GetEventApplyMethods(e);
                await applyMethod(e);
            }
            catch (Exception exception)
            {
                Logger.LogError($"(Error in Saga [{GetPrettyName()}] on handle event {e.GetPrettyName()}: {exception}", exception);
                await State.RollbackTransaction();

                throw;
            }

            await State.SaveState(this.GetPrimaryKeyString());

            await State.CommitTransaction();

            _isStarted = true;

            Logger.LogInformation($"(Saga [{GetPrettyName()}] handled event {e.GetPrettyName()}.");
        }
Пример #3
0
        internal async Task ProcessEventInternal(IDomainEvent data)
        {
            Logger.LogInformation($"(Subscriber [{GetSubscriberName()}] received event {data.GetPrettyName()}.");

            var sc = new SecurityContext(data.Metadata);


            //Проверим права
            var requiredUser = SecurityContext.IsUserRequiredFrom(data);

            if (requiredUser && !sc.IsAuthorized)
            {
                throw new UnauthorizedAccessException();
            }

            var requiredRole = SecurityContext.GetRolesFrom(data);

            if (requiredRole != null)
            {
                sc.HasRoles(requiredRole);
            }

            SecurityContext = sc;

            //Вызываем сагу для обработки события
            if (IsSync)
            {
                Logger.LogInformation($"(Subscriber [{GetSubscriberName()}] handling event sync {data.GetPrettyName()}...");

                try
                {
                    await HandleAsync((IDomainEvent <TIdentity, TEvent>) data).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Logger.LogError($"Error in Subscriber [{GetSubscriberName()}: {ex.Message}", ex);
                    return;
                }

                Logger.LogInformation($"(Subscriber [{GetSubscriberName()}] handle event {data.GetPrettyName()}.");
            }
            else
            {
                Logger.LogInformation($"(Subscriber [{GetSubscriberName()}] handling event async {data.GetPrettyName()}...");
                try
                {
                    var __ = HandleAsync((IDomainEvent <TIdentity, TEvent>)data).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Logger.LogError($"Error in Subscriber [{GetSubscriberName()}: {ex.Message}", ex);
                    return;
                }

                Logger.LogInformation($"(Subscriber [{GetSubscriberName()}] handle event {data.GetPrettyName()}.");
            }
        }