async Task ISagaRepository <TSaga> .Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next)
        {
            MessageSessionContext sessionContext;

            if (!context.TryGetPayload(out sessionContext))
            {
                throw new SagaException($"The session-based saga repository requires an active message session: {TypeMetadataCache<TSaga>.ShortName}",
                                        typeof(TSaga), typeof(T));
            }

            Guid sessionId;

            if (Guid.TryParse(sessionContext.SessionId, out sessionId))
            {
                context = new CorrelationIdConsumeContextProxy <T>(context, sessionId);
            }

            var saga = await ReadSagaState(sessionContext).ConfigureAwait(false);

            if (saga == null)
            {
                var missingSagaPipe = new MissingPipe <T>(next, WriteSagaState);

                await policy.Missing(context, missingSagaPipe).ConfigureAwait(false);
            }
            else
            {
                SagaConsumeContext <TSaga, T> sagaConsumeContext = new MessageSessionSagaConsumeContext <TSaga, T>(context, sessionContext, saga);

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("SAGA:{0}:{1} Existing {2}", TypeMetadataCache <TSaga> .ShortName, sessionContext.SessionId, TypeMetadataCache <T> .ShortName);
                }

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

                if (!sagaConsumeContext.IsCompleted)
                {
                    await WriteSagaState(sessionContext, saga).ConfigureAwait(false);

                    if (_log.IsDebugEnabled)
                    {
                        _log.DebugFormat("SAGA:{0}:{1} Updated {2}", TypeMetadataCache <TSaga> .ShortName, sessionContext.SessionId,
                                         TypeMetadataCache <T> .ShortName);
                    }
                }
            }
        }
            public async Task Send(SagaConsumeContext <TSaga, TMessage> context)
            {
                var sessionContext = context.GetPayload <MessageSessionContext>();

                var proxy = new MessageSessionSagaConsumeContext <TSaga, TMessage>(context, sessionContext, context.Saga);

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("SAGA:{0}:{1} Created {2}", TypeMetadataCache <TSaga> .ShortName, sessionContext.SessionId,
                                     TypeMetadataCache <TMessage> .ShortName);
                }

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

                    if (!proxy.IsCompleted)
                    {
                        await _writeSagaState(sessionContext, proxy.Saga).ConfigureAwait(false);

                        if (_log.IsDebugEnabled)
                        {
                            _log.DebugFormat("SAGA:{0}:{1} Saved {2}", TypeMetadataCache <TSaga> .ShortName, sessionContext.SessionId,
                                             TypeMetadataCache <TMessage> .ShortName);
                        }
                    }
                }
                catch (Exception)
                {
                    if (_log.IsDebugEnabled)
                    {
                        _log.DebugFormat("SAGA:{0}:{1} Unsaved(Fault) {2}", TypeMetadataCache <TSaga> .ShortName, sessionContext.SessionId,
                                         TypeMetadataCache <TMessage> .ShortName);
                    }

                    throw;
                }
            }