【问题标题】:Using MSMQ over HTTP. How to address the queue?通过 HTTP 使用 MSMQ。如何解决队列?
【发布时间】:2013-10-25 04:40:42
【问题描述】:

我目前正在尝试将 MSMQ 与 C# 和 .NET 一起使用以实现 IPC。 我试图了解它是如何工作的,但我对differences between Path name and Format name when accessing MSMQ queues 感到很困惑。 我在以下帖子中发现了一些类似的问题:

  1. MSMQ calls over HTTP not reaching destination queue
  2. How to setup MSMQ server so that it can be accessed over the Internet
  3. How to use MSMQ over http through the respective WCF binding?

但是,他们都在使用 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(); 时,我得到以下异常:

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 接收消息

标签: c# .net msmq


【解决方案1】:

指定 HTTP 或 HTTPS 协议的直接格式名称不能用于查看或接收消息,只能用于发送消息

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 2012-06-15
    • 2017-04-27
    • 2016-02-06
    • 2016-12-04
    • 1970-01-01
    • 2018-02-02
    • 2014-04-07
    • 1970-01-01
    相关资源
    最近更新 更多