Exemple #1
0
        async Task IFilter <ConsumeContext <TMessage> > .Send(ConsumeContext <TMessage> context, IPipe <ConsumeContext <TMessage> > next)
        {
            Stopwatch timer = Stopwatch.StartNew();

            try
            {
                ISagaQuery <TSaga> query = _queryFactory.CreateQuery(context);

                SagaQueryConsumeContext <TSaga, TMessage> queryContext = new SagaQueryConsumeContextProxy <TSaga, TMessage>(context, query);

                await Task.Yield();

                await _sagaRepository.SendQuery(queryContext, _policy, _messagePipe).ConfigureAwait(false);

                await next.Send(context).ConfigureAwait(false);

                await context.NotifyConsumed(timer.Elapsed, TypeMetadataCache <TSaga> .ShortName).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await context.NotifyFaulted(timer.Elapsed, TypeMetadataCache <TSaga> .ShortName, ex).ConfigureAwait(false);

                throw;
            }
        }
        ISagaQueryScopeContext <TSaga, T> ISagaScopeProvider <TSaga> .GetQueryScope <T>(SagaQueryConsumeContext <TSaga, T> context)
        {
            if (context.TryGetPayload <IServiceScope>(out var existingServiceScope))
            {
                existingServiceScope.UpdateScope(context);

                return(new ExistingSagaQueryScopeContext <TSaga, T>(context));
            }

            if (!context.TryGetPayload(out IServiceProvider serviceProvider))
            {
                serviceProvider = _serviceProvider;
            }

            var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            try
            {
                serviceScope.UpdateScope(context);

                var proxy = new SagaQueryConsumeContextProxy <TSaga, T>(context, new PayloadCacheScope(context), context.Query);
                proxy.UpdatePayload(serviceScope);

                foreach (Action <ConsumeContext> scopeAction in _scopeActions)
                {
                    scopeAction(proxy);
                }

                return(new CreatedSagaQueryScopeContext <IServiceScope, TSaga, T>(serviceScope, proxy));
            }
            catch
            {
                serviceScope.Dispose();

                throw;
            }
        }