Cannot determine whether a queue with the specified format name exists

前端 未结 5 1839
情歌与酒
情歌与酒 2021-02-04 04:28

I get the exception when executing the following code. Any ideas what is wrong?

string queueName = \"FormatName:Direct=TCP:1.1.1.1\\\\Private$\\\\test\";
Message         


        
5条回答
  •  盖世英雄少女心
    2021-02-04 05:14

    I ended up with the answers from Svix, Erwin van Dijk and Joseph Daigle combined. Additionally I checked for ArgumentException:

        /// 
        /// Checks if a (remote) Microsoft Message Queue is available
        /// 
        /// The name of the Message Queue.
        /// Returns true if the queue is available otherwise false.
        public static bool IsQueueAvailable(string queueName)
        {
            MessageQueue queue;
            try
            {
                queue = new MessageQueue(queueName);
                queue.Peek(new TimeSpan(0, 0, 5)); // wait max. 5 sec. to recieve first message from queue (reduce if necessary)
                return true;
            }
            catch (Exception ex)
            {
                if(ex is ArgumentException)
                {   // the provided queue name is wrong.
                    return false;
                }
                else if (ex is MessageQueueException)
                {   // if message queue exception occurs either the queue is avialable but without entries (check for peek timeout) or the queue does not exist or you don't have access.
                    return (((MessageQueueException)ex).MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout);
                }
                // any other error occurred.
                return false;
            }
        }
    

提交回复
热议问题