/// <summary>
 /// Removes the specified interceptor from the internal interceptor list.
 /// </summary>
 /// <param name="interceptor">The interceptor to remove.</param>
 public void RemoveInterceptor(MessageInterceptor interceptor)
 {
     lock (_activeInterceptors)
     {
         _activeInterceptors.Remove(interceptor);
     }
 }
 /// <summary>
 /// Adds the specified interceptor to the internal interceptor list.
 /// </summary>
 /// <param name="interceptor">The interceptor to add.</param>
 public void AddInterceptor(MessageInterceptor interceptor)
 {
     lock (_activeInterceptors)
     {
         _activeInterceptors.Add(interceptor);
     }
 }
Example #3
0
        /// <summary>
        /// Sends the specified message envelope and waits for the response in form of the specified message type.
        /// </summary>
        /// <param name="messageEnvelope">The message envelope to send.</param>
        /// <param name="messageId">The message identifier to use while waiting for the response.</param>
        /// <param name="responseType">The type of the response message to wait for.</param>
        /// <param name="waitTimeoutMilliseconds">The wait timeout milliseconds.</param>
        /// <returns>
        /// Requested response message if it was received; null otherwise.
        /// </returns>
        public MessageBase SendAndWaitForResponse(object messageEnvelope,
                                                  string messageId,
                                                  Type responseType,
                                                  int waitTimeoutMilliseconds = 0)
        {
            if (messageEnvelope == null)
            {
                throw new ArgumentException("Invalid messageEnvelope specified.");
            }

            if (responseType == null)
            {
                throw new ArgumentException("Invalid responseType specified.");
            }

            MessageInterceptor interceptor = null;

            try
            {
                lock (_syncLock)
                {
                    if (_messageObjectStream == null)
                    {
                        return(null);
                    }

                    interceptor = new MessageInterceptor(messageId, responseType);
                    AddInterceptor(interceptor);

                    if (_messageObjectStream.Write(messageEnvelope) == false)
                    {
                        throw new ApplicationException(string.Format("Sending '{0}' to storage system failed.", messageEnvelope.GetType().Name));
                    }
                }

                if (waitTimeoutMilliseconds == 0)
                {
                    interceptor.Wait();
                    return(interceptor.Message);
                }
                else
                {
                    if (interceptor.Wait(waitTimeoutMilliseconds))
                    {
                        return(interceptor.Message);
                    }

                    return(null);
                }
            }
            finally
            {
                if (interceptor != null)
                {
                    RemoveInterceptor(interceptor);
                    interceptor.Dispose();
                }
            }
        }
        /// <summary>
        /// Sends the specified message envelope and waits for the response in form of the specified message type.
        /// </summary>
        /// <param name="messageEnvelope">The message envelope to send.</param>
        /// <param name="messageId">The message identifier to use while waiting for the response.</param>
        /// <param name="responseType">The type of the response message to wait for.</param>
        /// <param name="waitTimeoutMilliseconds">The wait timeout milliseconds.</param>
        /// <returns>
        /// Requested response message if it was received; null otherwise.
        /// </returns>
        public MessageBase SendAndWaitForResponse(object messageEnvelope,
                                                  string messageId,
                                                  Type responseType,
                                                  int waitTimeoutMilliseconds = 0)
        {
            if (messageEnvelope == null)
            {
                throw new ArgumentException("Invalid messageEnvelope specified.");
            }

            if (responseType == null)
            {
                throw new ArgumentException("Invalid responseType specified.");
            }

            MessageInterceptor interceptor = null;

            try
            {
                lock (_syncLock)
                {
                    if (_messageObjectStream == null)
                    {
                        return(null);
                    }

                    this.Trace("Initialize a new Message Interceptor and add it to the internal interceptor list.");
                    interceptor = new MessageInterceptor(messageId, responseType);
                    AddInterceptor(interceptor);

                    this.Trace($"Start sending {messageEnvelope.GetType().Name} to Storage System.");
                    if (_messageObjectStream.Write(messageEnvelope) == false)
                    {
                        throw new ApplicationException(string.Format("Sending '{0}' to storage system failed.", messageEnvelope.GetType().Name));
                    }
                }

                this.Trace("Waiting for the message interception notification and return the intercepted message.");
                if (waitTimeoutMilliseconds == 0)
                {
                    interceptor.Wait();
                    return(interceptor.Message);
                }
                else
                {
                    if (interceptor.Wait(waitTimeoutMilliseconds))
                    {
                        return(interceptor.Message);
                    }

                    return(null);
                }
            }
            finally
            {
                if (interceptor != null)
                {
                    RemoveInterceptor(interceptor);
                    interceptor.Dispose();
                }
            }
        }