public async Task Send(SagaConsumeContext <TSaga, TMessage> context) { var instance = new SagaInstance <TSaga>(context.Saga); await instance.MarkInUse(context.CancellationToken).ConfigureAwait(false); try { var proxy = new InMemorySagaConsumeContext <TSaga, TMessage>(context, context.Saga, () => RemoveNewSaga(instance, context.CancellationToken)); if (_withinLock) { _repository.AddWithinLock(instance); } else { await _repository.Add(instance, context.CancellationToken).ConfigureAwait(false); } if (_log.IsDebugEnabled) { _log.DebugFormat("SAGA:{0}:{1} Added {2}", TypeMetadataCache <TSaga> .ShortName, context.Saga.CorrelationId, TypeMetadataCache <TMessage> .ShortName); } try { await _next.Send(proxy).ConfigureAwait(false); if (proxy.IsCompleted) { if (_log.IsDebugEnabled) { _log.DebugFormat("SAGA:{0}:{1} Removed {2}", TypeMetadataCache <TSaga> .ShortName, context.Saga.CorrelationId, TypeMetadataCache <TMessage> .ShortName); } await RemoveNewSaga(instance, context.CancellationToken).ConfigureAwait(false); } } catch (Exception) { if (_log.IsDebugEnabled) { _log.DebugFormat("SAGA:{0}:{1} Removed(Fault) {2}", TypeMetadataCache <TSaga> .ShortName, context.Saga.CorrelationId, TypeMetadataCache <TMessage> .ShortName); } await RemoveNewSaga(instance, context.CancellationToken).ConfigureAwait(false); throw; } } finally { instance.Release(); } }
public async Task Send(SagaConsumeContext <TSaga, TMessage> context) { var instance = new SagaInstance <TSaga>(context.Saga); await instance.MarkInUse(context.CancellationToken).ConfigureAwait(false); var activity = LogContext.IfEnabled(OperationName.Saga.Add)?.StartActivity(new { context.Saga.CorrelationId }); try { var sagaConsumeContext = new InMemorySagaConsumeContext <TSaga, TMessage>(context, context.Saga, () => RemoveNewSaga(instance, context.CancellationToken)); if (_withinLock) { _repository.AddWithinLock(instance); } else { await _repository.Add(instance, context.CancellationToken).ConfigureAwait(false); } sagaConsumeContext.LogAdded(); try { await _next.Send(sagaConsumeContext).ConfigureAwait(false); if (sagaConsumeContext.IsCompleted) { await RemoveNewSaga(instance, context.CancellationToken).ConfigureAwait(false); sagaConsumeContext.LogRemoved(); } } catch (Exception exception) { await RemoveNewSaga(instance, context.CancellationToken).ConfigureAwait(false); sagaConsumeContext.LogRemoved(exception); throw; } } finally { instance.Release(); activity?.Stop(); } }
public async Task Send(SagaConsumeContext <TSaga, TMessage> context) { var proxy = new InMemorySagaConsumeContext <TSaga, TMessage>(_repository, context, context.Saga, () => RemoveNewSaga(context.Saga, context.CancellationToken)); if (_withinLock) { _repository.AddWithinLock(context.Saga); } else { await _repository.Add(context.Saga, context.CancellationToken); } if (_log.IsDebugEnabled) { _log.DebugFormat("SAGA:{0}:{1} Added {2}", TypeMetadataCache <TSaga> .ShortName, context.Saga.CorrelationId, TypeMetadataCache <TMessage> .ShortName); } try { await _next.Send(proxy); if (proxy.IsCompleted) { await RemoveNewSaga(proxy.Saga, context.CancellationToken); } } catch (Exception) { if (_log.IsDebugEnabled) { _log.DebugFormat("SAGA:{0}:{1} Removed(Fault) {2}", TypeMetadataCache <TSaga> .ShortName, context.Saga.CorrelationId, TypeMetadataCache <TMessage> .ShortName); } await RemoveNewSaga(proxy.Saga, context.CancellationToken); throw; } }