开发者

How to create C# program that sends to local message queue.

开发者 https://www.devze.com 2023-04-07 22:33 出处:网络
I have a C# Program that is attempting to send to my own local Windows message queue.I have i开发者_如何学Cnstalled message queue and out of desperation gave all access rights to \"Everyone\" on both

I have a C# Program that is attempting to send to my own local Windows message queue. I have i开发者_如何学Cnstalled message queue and out of desperation gave all access rights to "Everyone" on both the Message Queue itself and the Public queue that I'm trying to write to. I have a form which has two text boxes and two buttons. One pair for sending and another pair for receiving. I click send and get no errors, it can detect that there is in fact a message queue of that name. But the send method seems to go off without actually getting it into the queue. Thanks for any help! Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Messaging;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public MessageQueue myNewPublicQueue;
    private static object lockObject = new object();
    /// <summary>
    /// 
    /// </summary>
    public Form1()
    {
        InitializeComponent();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void buttoSend_Click(object sender, EventArgs e)
    {
        if (MessageQueue.Exists(@".\queuename"))
        {
            myNewPublicQueue = new System.Messaging.MessageQueue(@".\queuename");
            System.Messaging.Message mm = new System.Messaging.Message();
            mm.Body = textBoxSend.Text;
            mm.Label = "First Test Message";
            myNewPublicQueue.Send(mm);

        }

    }

    private void buttonGet_Click(object sender, EventArgs e)
    {
        myNewPublicQueue.ReceiveCompleted += new          ReceiveCompletedEventHandler(queue_ReceiveCompleted);

        // Start listening.
        myNewPublicQueue.BeginReceive();

    }
    private void queue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
    {


        lock (lockObject)
        {
            // The message is plain text.
            string text = (string)e.Message.Body;
            Console.WriteLine("Message received: " + text);
            textBoxGet.Text = text;
        }

        // Listen for the next message.
        myNewPublicQueue.BeginReceive();
    }
    public enum MessageType
    {
        MESSAGE_TYPE_PLAIN_TEXT = 0,
        MESSAGE_TYPE_HELLO_WORLD = 1
    };

    private void Form1_Load(object sender, EventArgs e)
    {

    }





    }
}


When you call the MessageQueue.Send() method, check the Outgoing Queues folder in Message Queueing. You should see a temporary queue has been set up (note, this will only happen if the destination queue is on another machine to the sending machine). Check that queue and you should see your message in it. This means that you have addressed the queue incorrectly in your code.

Try this: create a private queue and address it using the format FORMATNAME:DIRECT=OS:<server name>\PRIVATE$\<queue name>. From your question it sounds like you really do not need to use public queues.

Also, don't bother checking if the queue exists unless you are intending to handle when it doesn't (for example by raising an exception or creating it programatically).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号