public async Task Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next) where T : class { if (!context.CorrelationId.HasValue) { throw new SagaException("The CorrelationId was not specified", typeof(TSaga), typeof(T)); } TSaga instance; if (policy.PreInsertInstance(context, out instance)) { await PreInsertSagaInstance(context, instance).ConfigureAwait(false); } if (instance == null) { instance = await _collection.Find(x => x.CorrelationId == context.CorrelationId).SingleOrDefaultAsync(context.CancellationToken).ConfigureAwait(false); } if (instance == null) { var missingSagaPipe = new MissingPipe <TSaga, T>(_collection, next, _mongoDbSagaConsumeContextFactory); await policy.Missing(context, missingSagaPipe).ConfigureAwait(false); } else { await SendToInstance(context, policy, next, instance).ConfigureAwait(false); } }
public async Task SendQuery <T>(SagaQueryConsumeContext <TSaga, T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next) where T : class { try { var sagaInstances = await _collection.Find(context.Query.FilterExpression).ToListAsync().ConfigureAwait(false); if (!sagaInstances.Any()) { var missingPipe = new MissingPipe <TSaga, T>(_collection, next, _mongoDbSagaConsumeContextFactory); await policy.Missing(context, missingPipe).ConfigureAwait(false); } else { foreach (var instance in sagaInstances) { await SendToInstance(context, policy, next, instance).ConfigureAwait(false); } } } catch (SagaException sex) { if (_log.IsErrorEnabled) { _log.Error("Saga Exception Occurred", sex); } } catch (Exception ex) { if (_log.IsErrorEnabled) { _log.Error($"SAGA:{TypeMetadataCache<TSaga>.ShortName} Exception {TypeMetadataCache<T>.ShortName}", ex); } throw new SagaException(ex.Message, typeof(TSaga), typeof(T), Guid.Empty, ex); } }