MSMQ Serializing

R

Richard Atkinson

I would appreciate help on the Microsoft Message Queue.

Basically I would like to Serialize an entire class into a message queue and
then reinstantiate the class in another application from the message queue
with all properties, variables etc in tact. What is the best way to do
this ie which formatter to use and how do you deal with arrays collections
properties etc defined in the class since there seems to be a limitation on
what you can serialize.

Thanks
 
P

Peter Huang [MSFT]

Hi

Here is a link about the formatting choosing suggestion. Basically the xml
ones will give more flexible, the binary ones will give more performance.

Send MSMQ Messages Securely Across the Internet with HTTP and SOAP
http://msdn.microsoft.com/msdnmag/issues/03/12/MSMQandNET/default.aspx

Here is a link about Object Serialization

Object Serialization in Visual Basic .NET
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/ht
ml/vbnet09252001.asp

The link below is about how to send message using MSMQ in .net
BinaryMessageFormatter Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemmessagingbinarymessageformatterclasstopic.asp

The link below is about how to do xml Serialization if you want to do the
xml format in msmq.
INFO: Roadmap for XML Serialization in the .NET Framework
http://support.microsoft.com/kb/314150/EN-US/

Here is some code snippet. Hope this will give some idea to get started.
[The Object definition sender side]
[System.Xml.Serialization.XmlRoot("RootName")]
public class TestObject
{
[XmlElement("MyArrayList",typeof(string))]
public ArrayList Info=new ArrayList();
public TestObject()
{
Info.Add("Hello");
}
}

[The Object definition receiver side]
[System.Xml.Serialization.XmlRoot("RootName")]
public class TestObject2
{
[XmlElement("MyArrayList",typeof(string))]
public ArrayList Info2;
}

[Code send and receive]
MyNewQueue myNewQueue = new MyNewQueue();
private void button1_Click(object sender, System.EventArgs e)
{
// Create a queue on the local computer.
MyNewQueue.CreateQueue(".\\myQueue");

// Send a message to a queue.
myNewQueue.SendMessage(new TestObject());

}

private void button2_Click(object sender, System.EventArgs e)
{
// Receive a message from a queue.
TestObject2 o2 = (TestObject2)myNewQueue.ReceiveMessage();

this.textBox2.Text = o2.Info2[0].ToString();
}

[The MyNewQueue class]
using System;
using System.Messaging;
using System.Drawing;
using System.IO;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
namespace TestMSMQ
{
/// <summary>
/// Provides a container class for the example.
/// </summary>
///

public class MyNewQueue
{

//**************************************************
// Creates a new queue.
//**************************************************

public static void CreateQueue(string queuePath)
{
try
{
if(!MessageQueue.Exists(queuePath))
{
MessageQueue.Create(queuePath);
}
else
{
Console.WriteLine(queuePath + " already exists.");
}
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}

}


public void SendMessage(object o)
{
try
{

// Connect to a queue on the local computer.
MessageQueue myQueue = new MessageQueue(".\\myQueue");

Message myMessage = new Message(o, new XmlMessageFormatter(new
Type[]{typeof(TestObject)}));

// Send to the queue.
myQueue.Send(myMessage);
}
catch(ArgumentException e)
{
Console.WriteLine(e.Message);

}

return;
}


public object ReceiveMessage()
{

try
{

// Connect to the a queue on the local computer.
MessageQueue myQueue = new MessageQueue(".\\myQueue");

// Set the formatter to indicate body contains an Order.
myQueue.Formatter = new XmlMessageFormatter(new
Type[]{typeof(TestObject2)});

// Receive and format the message.
System.Messaging.Message myMessage = myQueue.Receive();
object o = (object)myMessage.Body;
return o;
}

catch (MessageQueueException)
{
// Handle Message Queuing exceptions.

}

// Handle invalid serialization format.
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}

catch (IOException e)
{
// Handle file access exceptions.
}

// Catch other exceptions as necessary.
return new object();

}
}
}

Hope this helps.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top