/// <summary>
        /// Если в кеше, есть ответное сообщение на входящее сообщение, тогда возвращает объект из кеша.
        /// </summary>
        /// <param name="exchange">Конвейер обработки сообщений.</param>
        /// <param name="invoker">Фильтры вызывающий конвейер.</param>
        /// <returns>Задача обработки сообщений.</returns>
        public Task<MessageExchange> Process(MessageExchange exchange, MessageExchangeFilterInvoker invoker)
        {
            if (!exchange.IsIncompleteRequest)
            {
                return invoker.Continue(exchange);
            }

            string hash = this.hasher.CalculateHashOf(exchange.Out).ToString();
            Maybe<object> cached = this.cacheProvider.Find<object>(hash);
            if (cached.HasValue)
            {
                exchange.In = new Message(MessageLabel.Empty, cached.Value);
                return Filter.Result(exchange);
            }

            return invoker.Continue(exchange)
                .ContinueWith(
                    t =>
                        {
                            MessageExchange resultExchange = t.Result;
                            if (!resultExchange.IsCompleteRequest)
                            {
                                return resultExchange;
                            }

                            string expiresHeader = Headers.GetString(resultExchange.In.Headers, Headers.Expires);
                            if (!string.IsNullOrEmpty(expiresHeader))
                            {
                                Expires expiration = Expires.Parse(expiresHeader);

                                if (expiration.Period.HasValue)
                                {
                                    this.cacheProvider.Put(hash, resultExchange.In.Payload, expiration.Period.Value);
                                }
                                else
                                {
                                    this.cacheProvider.Put(hash, resultExchange.In.Payload, expiration.Date.Value);
                                }
                            }

                            return resultExchange;
                        });
        }
            public void should_invoke_starting_with_recent()
            {
                var log = new List<string>();
                var filters = new[] { new LoggingFilter("A", log), new LoggingFilter("B", log), new LoggingFilter("C", log) };
                var exchange = new MessageExchange(new Message("msg".ToMessageLabel(), new object()));
                var invoker = new MessageExchangeFilterInvoker(filters);

                invoker.Process(exchange).
                    Wait(1.Seconds());

                log.Should().
                    BeEquivalentTo(new object[] { "C: before", "B: before", "A: before", "A: after", "B: after", "C: after" });
            }
            /// <summary>
            /// The process.
            /// </summary>
            /// <param name="exchange">
            /// The exchange.
            /// </param>
            /// <param name="invoker">
            /// The invoker.
            /// </param>
            /// <returns>
            /// The <see cref="Task"/>.
            /// </returns>
            public Task<MessageExchange> Process(MessageExchange exchange, MessageExchangeFilterInvoker invoker)
            {
                this._log.Add(this._name + ": before");

                if (this._stop)
                {
                    return Filter.Result(exchange);
                }

                return invoker.Continue(exchange).
                    ContinueWith(
                        t =>
                            {
                                this._log.Add(this._name + ": after");
                                return t.Result;
                            });
            }
Beispiel #4
0
 /// <summary>
 /// The process.
 /// </summary>
 /// <param name="exchange">
 /// The exchange.
 /// </param>
 /// <param name="invoker">
 /// The invoker.
 /// </param>
 /// <returns>
 /// The <see cref="Task"/>.
 /// </returns>
 public Task <MessageExchange> Process(MessageExchange exchange, MessageExchangeFilterInvoker invoker)
 {
     return(this._sendingAction(exchange).ContinueWith(_ => invoker.Continue(exchange).Result));
 }
 /// <summary>
 /// The process.
 /// </summary>
 /// <param name="exchange">
 /// The exchange.
 /// </param>
 /// <param name="invoker">
 /// The invoker.
 /// </param>
 /// <returns>
 /// The <see cref="Task"/>.
 /// </returns>
 public Task<MessageExchange> Process(MessageExchange exchange, MessageExchangeFilterInvoker invoker)
 {
     return this._sendingAction(exchange).ContinueWith(_ => invoker.Continue(exchange).Result);
 }