通过HTTP使用MSMQ。 如何解决队列?

我目前正在尝试将MSMQ与C#和.NET一起使用以实现IPC。 我试图了解它是如何工作的,我对访问MSMQ队列时路径名和格式名之间的差异感到很困惑。 我在以下post中发现了一些类似的问题:

  1. MSMQ通过HTTP调用未到达目标队列
  2. 如何设置MSMQ服务器,以便可以通过Internet访问它
  3. 如何通过相应的WCF绑定使用MSMQ over http?

但是,他们都使用MSMQ和WCF,我现在不想使用WCF。

我想要实现的目标如下:

客户端:通过http将数据发送到队列。

服务器:通过http从队列接收数据。

我的观点是,我希望服务器,客户端和队列都托管在可能不同的计算机中。 (现在我正在测试同一台机器上的所有东西)。

这里我有以下代码,它们展示了我的想法:

首先,我创建队列:

if(!System.Messaging.MessageQueue.Exists(@".\Private$\SimplestExamplePrivateQueue"); System.Messaging.MessageQueue.Create(@".\Private$\SimplestExamplePrivateQueue"); 

客户代码:

然后,在客户端,我有一个回调函数,当用户按下按钮以发送消息时调用该函数。

 private void button1_Click(object sender, System.EventArgs e) { try { // Create a connection to the queue System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(@"FormatName:Direct=http://localhost/msmq/Private$/SimplestExamplePrivateQueue"); // Create a point object to send Point myPoint = new Point (Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text)) ; // Send object mq.Send (myPoint) ; } // Catch the exception that signals all types of error // from the message queueing subsystem. Report error // to the user. catch (System.Messaging.MessageQueueException mqx) { MessageBox.Show (mqx.Message) ; } 

直到这里的一切都很好。

服务器代码:

然后,我有一个按钮,它调用一个回调函数来同步从服务器端的队列中读取一条消息:

 private void button1_Click(object sender, EventArgs e) { try { // Create a connection to the queue var mq = new MessageQueue(@"Direct=http://localhost/msmq/Private$/SimplestExamplePrivateQueue"); // Set the queue's formatter to decode Point objects mq.Formatter = new XmlMessageFormatter(new[] {typeof (Point)}); // Receive message synchronously Message msg = mq.Receive(); // Convert received message to object that we think was sent var pt = (Point) msg.Body; // Display it to the user MessageBox.Show(pt.ToString(), "Received Point"); } // Report any exceptions to the user. A timeout would cause such // an exception catch (Exception x) { MessageBox.Show(x.Message); } } 

在我(有限)对MSMQ的理解中,这应该有效。 但是,当我调用Message msg = mq.Receive(); 我得到以下exception:

 The specified format name does not support the requested operation. For example, a direct queue format name cannot be deleted. 

和堆栈跟踪:

 at System.Messaging.MessageQueue.MQCacheableInfo.get_ReadHandle() at System.Messaging.MessageQueue.StaleSafeReceiveMessage(UInt32 timeout, Int32 action, MQPROPS properties, NativeOverlapped* overlapped, ReceiveCallback receiveCallback, CursorHandle cursorHandle, IntPtr transaction) at System.Messaging.MessageQueue.ReceiveCurrent(TimeSpan timeout, Int32 action, CursorHandle cursor, MessagePropertyFilter filter, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType) at System.Messaging.MessageQueue.Receive() at InternetQueueingRecipient.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\felipedalcin\Documents\MSMQ\MSMQandNET\InternetQueuing\InternetQueueingRecipient\Form1.cs:line 85 

有没有人知道如何调试这个,或者即使我想做的事情是通过这些手段可以实现的?

您无法通过HTTP接收消息。

说明