Esempio n. 1
0
            public async Task Send(SagaConsumeContext <TSaga, TMessage> context)
            {
                context.LogAdded();

                SagaConsumeContext <TSaga, TMessage> proxy = new NHibernateSagaConsumeContext <TSaga, TMessage>(_session, context, context.Saga);

                await _next.Send(proxy).ConfigureAwait(false);

                if (!proxy.IsCompleted)
                {
                    await _session.SaveAsync(context.Saga).ConfigureAwait(false);
                }
            }
            public async Task Send(SagaConsumeContext <TSaga, TMessage> context)
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("SAGA:{0}:{1} Added {2}", TypeMetadataCache <TSaga> .ShortName, context.Saga.CorrelationId,
                                     TypeMetadataCache <TMessage> .ShortName);
                }

                SagaConsumeContext <TSaga, TMessage> proxy = new NHibernateSagaConsumeContext <TSaga, TMessage>(_session, context, context.Saga);

                await _next.Send(proxy).ConfigureAwait(false);

                if (!proxy.IsCompleted)
                {
                    _session.Save(context.Saga);
                }
            }
Esempio n. 3
0
        static Task SendToInstance <T>(SagaQueryConsumeContext <TSaga, T> context, ISagaPolicy <TSaga, T> policy, TSaga instance,
                                       IPipe <SagaConsumeContext <TSaga, T> > next, ISession session)
            where T : class
        {
            try
            {
                var sagaConsumeContext = new NHibernateSagaConsumeContext <TSaga, T>(session, context, instance);

                sagaConsumeContext.LogUsed();

                return(policy.Existing(sagaConsumeContext, next));
            }
            catch (SagaException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SagaException(ex.Message, typeof(TSaga), typeof(T), instance.CorrelationId, ex);
            }
        }
        async Task SendToInstance <T>(SagaQueryConsumeContext <TSaga, T> context, ISagaPolicy <TSaga, T> policy, TSaga instance,
                                      IPipe <SagaConsumeContext <TSaga, T> > next, ISession session)
            where T : class
        {
            try
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("SAGA:{0}:{1} Used {2}", TypeMetadataCache <TSaga> .ShortName, instance.CorrelationId, TypeMetadataCache <T> .ShortName);
                }

                var sagaConsumeContext = new NHibernateSagaConsumeContext <TSaga, T>(session, context, instance);

                await policy.Existing(sagaConsumeContext, next).ConfigureAwait(false);
            }
            catch (SagaException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SagaException(ex.Message, typeof(TSaga), typeof(T), instance.CorrelationId, ex);
            }
        }
        async Task ISagaRepository <TSaga> .Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next)
        {
            if (!context.CorrelationId.HasValue)
            {
                throw new SagaException("The CorrelationId was not specified", typeof(TSaga), typeof(T));
            }

            var sagaId = context.CorrelationId.Value;

            using (var session = _sessionFactory.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var inserted = false;

                    TSaga instance;
                    if (policy.PreInsertInstance(context, out instance))
                    {
                        inserted = PreInsertSagaInstance <T>(session, instance);
                    }

                    try
                    {
                        if (instance == null)
                        {
                            instance = session.Get <TSaga>(sagaId, LockMode.Upgrade);
                        }
                        if (instance == null)
                        {
                            var missingSagaPipe = new MissingPipe <T>(session, next);

                            await policy.Missing(context, missingSagaPipe).ConfigureAwait(false);
                        }
                        else
                        {
                            if (_log.IsDebugEnabled)
                            {
                                _log.DebugFormat("SAGA:{0}:{1} Used {2}", TypeMetadataCache <TSaga> .ShortName, instance.CorrelationId, TypeMetadataCache <T> .ShortName);
                            }

                            var sagaConsumeContext = new NHibernateSagaConsumeContext <TSaga, T>(session, context, instance);

                            await policy.Existing(sagaConsumeContext, next).ConfigureAwait(false);

                            if (inserted && !sagaConsumeContext.IsCompleted)
                            {
                                session.Update(instance);
                            }
                        }

                        if (transaction.IsActive)
                        {
                            transaction.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.Error($"SAGA:{TypeMetadataCache<TSaga>.ShortName} Exception {TypeMetadataCache<T>.ShortName}", ex);
                        }

                        if (transaction.IsActive)
                        {
                            transaction.Rollback();
                        }

                        throw;
                    }
                }
        }
Esempio n. 6
0
        async Task ISagaRepository <TSaga> .Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next)
        {
            if (!context.CorrelationId.HasValue)
            {
                throw new SagaException("The CorrelationId was not specified", typeof(TSaga), typeof(T));
            }

            var sagaId = context.CorrelationId.Value;

            using (var session = _sessionFactory.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var inserted = false;

                    if (policy.PreInsertInstance(context, out var instance))
                    {
                        inserted = await PreInsertSagaInstance(context, session, instance).ConfigureAwait(false);
                    }

                    try
                    {
                        if (instance == null)
                        {
                            instance = await session.GetAsync <TSaga>(sagaId, LockMode.Upgrade).ConfigureAwait(false);
                        }
                        if (instance == null)
                        {
                            var missingSagaPipe = new MissingPipe <T>(session, next);

                            await policy.Missing(context, missingSagaPipe).ConfigureAwait(false);
                        }
                        else
                        {
                            var sagaConsumeContext = new NHibernateSagaConsumeContext <TSaga, T>(session, context, instance);

                            sagaConsumeContext.LogUsed();

                            await policy.Existing(sagaConsumeContext, next).ConfigureAwait(false);

                            if (inserted && !sagaConsumeContext.IsCompleted)
                            {
                                await session.UpdateAsync(instance).ConfigureAwait(false);
                            }
                        }

                        if (transaction.IsActive)
                        {
                            await transaction.CommitAsync().ConfigureAwait(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        context.LogFault(this, ex, instance?.CorrelationId);

                        if (transaction.IsActive)
                        {
                            await transaction.RollbackAsync().ConfigureAwait(false);
                        }

                        throw;
                    }
                }
        }