I have a question about MSMQ as I'm new to using this system in the .NET framework.
Here is my following application that I run 2 instances of to test the ability to communicate through the MSMQ.
It's just a basic form with 2 textboxes, and 2 buttons, first textbox is to "send" or put on the queue (with a send button), second is to receive (take from the queue with a receive button).
My question is, why before I specified the message.Formatter did it not allow the apps to take and put on the queue, before I added the formatter I was receiving an error of "Cannot find a formatter capable of reading this message" IF and only if you tried for the first time to remove the message from the other application, not if within the same instance of the application you send and received, that worked with no problem.
And even stranger, after each application sent and received within itself (meaning sent something to the queue and received it back, but not receiving something the other instance of the application put on the queue) they could then read what was put on the queue by the other application.
Shouldn't the formatter be default for both? Why should I have to declare it?
Expand|Select|Wrap|Line Numbers
- namespace MSMQSender
- {
- public partial class Form1 : Form
- {
- private MessageQueue messageQueue1;
- public Form1()
- {
- try
- {
- InitializeComponent();
- string queuePath = "******\\private$\\test1";
- if (MessageQueue.Exists(queuePath))
- messageQueue1 = new MessageQueue(queuePath);
- else
- messageQueue1 = MessageQueue.Create(queuePath);
- // messageQueue1.Purge();
- messageQueue1.DenySharedReceive = false;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// Send button
- private void button1_Click(object sender, EventArgs e)
- {
- messageQueue1.Send(textBox1.Text);
- }
- /// Receive button
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- if (messageQueue1.Peek(TimeSpan.FromMilliseconds(100)) != null)
- {
- System.Messaging.Message message = messageQueue1.Receive();
- string myMessage = ConvertToString(message.Body);
- textBox2.Text = myMessage;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
what fixed the issue was adding this:
Expand|Select|Wrap|Line Numbers
- message.Formatter = new System.Messaging.XmlMessageFormatter(new string[] { "System.String" });