Ejemplo n.º 1
0
        /// <summary>
        /// Dispatch a message through the pipeline. If the message will be consumed, the accept function
        /// is called to allow the endpoint to acknowledge the message reception if applicable
        /// </summary>
        /// <param name="pipeline">The pipeline instance</param>
        /// <param name="message">The message to dispatch</param>
        /// <param name="acknowledge">The function to call if the message will be consumed by the pipeline</param>
        /// <returns>whether the message was consumed</returns>
        public static bool Dispatch <T>(this IInboundMessagePipeline pipeline, T message, Func <T, bool> acknowledge)
            where T : class
        {
            bool consumed = false;

            using (var bodyStream = new MemoryStream())
            {
                ReceiveContext receiveContext = ReceiveContext.FromBodyStream(bodyStream);
                pipeline.Configure(x => receiveContext.SetBus(x.Bus));
                var context = new ConsumeContext <T>(receiveContext, message);

                using (context.CreateScope())
                {
                    foreach (var consumer in pipeline.Enumerate(context))
                    {
                        if (!acknowledge(message))
                        {
                            return(false);
                        }

                        acknowledge = x => true;

                        consumed = true;

                        consumer(context);
                    }
                }
            }

            return(consumed);
        }
Ejemplo n.º 2
0
        public IEnumerable <Action <IConsumeContext <Distributed <TMessage> > > > Enumerate(
            IConsumeContext <Distributed <TMessage> > context)
        {
            if (!_worker.CanAcceptMessage(context.Message))
            {
                return(Enumerable.Empty <Action <IConsumeContext <Distributed <TMessage> > > >());
            }

            var payloadContext = new ConsumeContext <TMessage>(context.BaseContext, context.Message.Payload);

            payloadContext.SetResponseAddress(context.Message.ResponseAddress);

            return(_sink.Enumerate(payloadContext).Select(action => (Action <IConsumeContext <Distributed <TMessage> > >)(m =>
            {
                _worker.IncrementInProgress();
                try
                {
                    _worker.ConsumingMessage(context.Message);

                    using (payloadContext.CreateScope())
                    {
                        action(payloadContext);
                    }
                }
                finally
                {
                    _worker.DecrementInProgress();
                }
            })));
        }
Ejemplo n.º 3
0
        async Task ISagaRepository <TSaga> .Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next)
        {
            using (var lifetimeScope = _scope.BeginLifetimeScope(_name, x => ConfigureScope(x, context)))
            {
                ConsumeContext <T> proxy = context.CreateScope(lifetimeScope);

                await _repository.Send(proxy, policy, next).ConfigureAwait(false);
            }
        }
Ejemplo n.º 4
0
        public async Task Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next) where T : class
        {
            using (var scope = this._services.CreateScope())
            {
                ConsumeContext <T> proxy = context.CreateScope(scope);

                await this._repository.Send(proxy, policy, next).ConfigureAwait(false);
            }
        }
Ejemplo n.º 5
0
        public async Task Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next) where T : class
        {
            using (var container = _container.CreateChildContainer())
            {
                ConsumeContext <T> proxy = context.CreateScope(container);

                await _repository.Send(proxy, policy, next).ConfigureAwait(false);
            }
        }
        public async Task Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next) where T : class
        {
            using (var scope = _container.BeginExecutionContextScope())
            {
                ConsumeContext <T> proxy = context.CreateScope(scope);

                await _repository.Send(proxy, policy, next).ConfigureAwait(false);
            }
        }
        async Task ISagaRepository <TSaga> .Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next)
        {
            using (var lifetimeScope = _scope.BeginLifetimeScope(_name, x => ConfigureScope(x, context)))
            {
                ConsumeContext <T> proxy = context.CreateScope(lifetimeScope);

                proxy.GetOrAddPayload <IStateMachineActivityFactory>(() => new AutofacStateMachineActivityFactory());

                await _repository.Send(proxy, policy, next).ConfigureAwait(false);
            }
        }
Ejemplo n.º 8
0
        async Task ISagaRepository <TSaga> .Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next)
        {
            using (var nestedContainer = _container.GetNestedContainer())
            {
                ConsumeContext <T> proxy = context.CreateScope(nestedContainer);

                nestedContainer.Inject(proxy);

                await _repository.Send(proxy, policy, next).ConfigureAwait(false);
            }
        }
Ejemplo n.º 9
0
        async Task ISagaRepository <TSaga> .Send <T>(ConsumeContext <T> context, ISagaPolicy <TSaga, T> policy, IPipe <SagaConsumeContext <TSaga, T> > next)
        {
            using (var nestedContainer = _container.GetNestedContainer())
            {
                ConsumeContext <T> proxy = context.CreateScope(nestedContainer);

                nestedContainer.Configure(x =>
                {
                    x.For <ConsumeContext>()
                    .Use(proxy);
                    x.For <ConsumeContext <T> >()
                    .Use(proxy);
                });

                proxy.GetOrAddPayload <IStateMachineActivityFactory>(() => new StructureMapStateMachineActivityFactory());

                await _repository.Send(proxy, policy, next).ConfigureAwait(false);
            }
        }