简体   繁体   中英

MSMQ custom message format

I would like to make message in MSMQ which will have text for example

<order><data id="5" color="blue"/></order>

This is standard XML. So far I have made Serializable class

[Serializable]
public class order
string id
string color

I am using BinaryFormatter. When i check the message.BodyStream there are some chars which are not supposed to be there( 00,01,FF ), then I cannot receive this message without error.

This task seems to be simple, just put text

<order><data id="5" color="blue"/></order> 

into msmq.

Mine whole important code:

public static void Send()
    {
        using (message = new Message())
        {
            request req = new request("1", "blue");

                message.Recoverable = true;
                message.Body = req.ToString();
                message.Formatter = new BinaryMessageFormatter();
                using (msmq = new MessageQueue(@".\Private$\testrfid"))
                {
                    msmq.Formatter = new BinaryMessageFormatter();
                    msmq.Send(message, MessageQueueTransactionType.None);
                }
        }
    }

[Serializable]
public class request
{
    private readonly string _order;
    private readonly string _color;

    public request(string order, string color)
    {
        _order = order;
        _color = color;
    }
    public request()
    { }
    public string Order
    {
        get { return _order; }
    }
    public string Color
    {
        get { return _color; }
    }

    public override string ToString()
    {
        return string.Format(@"<request> <job order = ""{0}"" color = ""{1}"" /> </request>",_order,_color);
    }
}

Your question isn't very clear at all; you can send any type of message you like to MSMQ, so long as you use the BinaryMessageFormatter. Here's an example:

string error = "Some error message I want to log";

using (MessageQueue MQ = new MessageQueue(@".\Private$\Your.Queue.Name"))
{
    BinaryMessageFormatter formatter = new BinaryMessageFormatter();
    System.Messaging.Message mqMessage = new System.Messaging.Message(error, formatter);
    MQ.Send(mqMessage, MessageQueueTransactionType.Single);
    MQ.Close();
}

I have not found reason why the Message.Body contains these ascii characters before the string I pass to Body. I just fill directly BodyStream instead of Body and let it convert itself:

Message.BodyStream = new MemoryStream(Encoding.ASCII.GetBytes(string i want to put as Body))

Then the message is simply only the string with nothing else.

You don't need the serializable class to send a string to a message queue.

Since you are using the BinaryMessageFormatter, you must first convert your string to a byte array using a text encoder eg

message.Body = new UTF8Encoding().GetBytes(req.ToString());

I'm just using UTF8 as an example, you can use whatever encoding you like.

Then when you read the message from the queue, remember to use the same encoding to get your string back eg

string myString = new UTF8Encoding().GetString(message.Body);

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM