예제 #1
0
        /// <summary>
        /// Creates a <see cref="ISentMessage"/> wrapper for the specified
        /// <paramref name="message"/> and stores it in cache
        /// </summary>
        /// <param name="message">The recently sent message</param>
        /// <returns>Returns a <see cref="ISentMessage"/> that can be used to listen
        /// for replies</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="message"/>
        /// is <c>null</c></exception>
        public ISentMessage CreateSentMessage(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            CheckDisposed();

            var messageId             = message.Headers.MessageId;
            var replyStreamExpiration = DateTime.UtcNow.Add(_replyTimeout);

#if NET452 || NET461
            var newReplyStream = new ReplyStream();
            var replyStream    = (ReplyStream)_cache.AddOrGetExisting(messageId, newReplyStream, replyStreamExpiration);
            // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
            if (replyStream == null)
            {
                // MemoryCache.AddOrGetExisting returns null if the key does not
                // already exist, so use the one we just created. See:
                // http://msdn.microsoft.com/en-us/library/dd988741%28v=vs.110%29.aspx
                replyStream = newReplyStream;
            }
            return(new SentMessageWithCachedReplies(messageId, replyStream));
#endif
#if NETSTANDARD2_0
            var replyStream = _cache.GetOrCreate(messageId, entry =>
            {
                entry.AbsoluteExpiration = replyStreamExpiration;
                return(new ReplyStream());
            });
            return(new SentMessageWithCachedReplies(messageId, replyStream));
#endif
        }
        public SentMessageWithCachedReplies(MessageId messageId, ReplyStream replyStream)
        {
            if (messageId == null)
            {
                throw new ArgumentNullException(nameof(messageId));
            }

            MessageId    = messageId;
            _replyStream = replyStream ?? throw new ArgumentNullException(nameof(replyStream));
        }