IT Community - Software Programming, Web Development and Technical Support

Microsoft Messsage Queuing (MSMQ) in C#

This is a discussion on Microsoft Messsage Queuing (MSMQ) in C# within the C# Programming forums, part of the Software Development category; There are several different kinds of queues available in the C# ASP .NET framework with the most popular one being ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-22-2008, 01:00 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Microsoft Messsage Queuing (MSMQ) in C#

There are several different kinds of queues available in the C# ASP .NET framework with the most popular one being the standard Queue collection object. While these collections are easy to work with and fairly robust they are still stored in memory and therefore temporary. Often, in enterprise application design, more stringent means of managing and storing temporary data are required.

In the case of receiving timely data to be processed, using a queue in your software design makes sense. When the data needs to be guaranteed for delivery and protected from loss, the Microsoft Message Queue MSMQ provides a scalable easy solution.
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-22-2008, 01:04 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Creating a Message Queue and Add Private Messages in Local System:
private string queueName = "private$\\test1";
try
{

System.Messaging.Message msg = new System.Messaging.Message();
msg.Body = "Hello";
System.Messaging.MessageQueue[] privateMessageQueues = System.Messaging.MessageQueue.GetPrivateQueuesByMa chine("localhost");
bool flag = true;
foreach (System.Messaging.MessageQueue privareMessageQueue in privateMessageQueues)
{
if (privareMessageQueue.QueueName == queueName)
{
flag = false;
break;
}
}
if (flag)
{
System.Messaging.MessageQueue.Create(".\\" + queueName);
}
System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(".\\" + queueName);
msgQ.Send(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
__________________
S.Balasubramanian
Nothing is impossible

Last edited by Balasubramanian.S : 02-22-2008 at 01:10 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-22-2008, 01:06 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Receiving Messages from Message Queue from Local System:
private string queueName = "private$\\test1";
System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(".\\" + queueName);
Object o = new Object();
System.Type[] arrTypes = new System.Type[2];
arrTypes[0] = TypeOf(String);
arrTypes[1] = o.GetType();
msgQ.Formatter = new XmlMessageFormatter(arrTypes);
string message= ((string)msgQ.Receive().Body);
MessageBox.Show(message.ToString(), "Message Received!");
__________________
S.Balasubramanian
Nothing is impossible

Last edited by Balasubramanian.S : 02-24-2008 at 09:16 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-24-2008, 09:03 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Creating a Public Message Queue and add public messages in this queue:

private string queueName = ".\\test1";


try
{

System.Messaging.Message msg = new System.Messaging.Message();
msg.Body = "Hello";
if (!System.Messaging.MessageQueue.Exists(queueName))
{
System.Messaging.MessageQueue.Create(queueName);
}
System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(queueName);
msgQ.Send(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-24-2008, 09:08 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Creating a Message Queue in another System (Through Lan) :

private string queueName = "Machine Name\\test1";
try
{

System.Messaging.Message msg = new System.Messaging.Message();
msg.Body = "Hello';
if (!System.Messaging.MessageQueue.Exists(queueName))
{
System.Messaging.MessageQueue.Create(queueName);
}
System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(queueName);
msgQ.SetPermissions(userName, MessageQueueAccessRights.FullControl);
msgQ.Send(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}


Machine Name is the name of the computer. If you give the IP address of the system, Message Queue could not be identified. Only, System name is identified.

userName is the login name of the system. This user only can retrieve or add messages in this queue.
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-24-2008, 09:15 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Retrieving a message from Message Queue from another System (Through Lan) :
private string queueName = "system name\\test1";

try
{
if(System.Messaging.MessageQueue.Exists(queueName) )
{
string message;
Object o = new Object();
System.Type[] arrTypes = new System.Type[2];
arrTypes[0] = typeof(string);
arrTypes[1] = o.GetType();
messageQ.Formatter = new XmlMessageFormatter(arrTypes);
message = ((string)messageQ.Receive().Body);
MessageBox.Show(message.ToString(), "Message eceived!");
}
else
{
MessageBox.Show("Queue does not exist");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-26-2008, 05:41 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

MSMQ:

Only public queues can be accessed by another system. We can get the count
of Private Queues but not the messages in this queue from another system.
Credential is very important in MSMQ. The specified user only can read the messages from the queue. The credential is set when the queue is created.

msgQ.SetPermissions(userName, MessageQueueAccessRights.FullControl);
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-26-2008, 05:48 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Enable MSMQ: (XP OS):

Start -------> ControlPanel -------> Add Or Remove Programs ---- > Add Or Remove Windows Components ------ > Message Queuing

Enable MSMQ: (Windows 2003 Server):

Start -------> ControlPanel -------> Add Or Remove Programs ---- > Add Or Remove Windows Components ------ > Application Servers ---- > Message Queuing
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-26-2008, 05:55 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

MSMQ takes advantage of various built in security features in Windows operating systems:

* Access Control to restrict user access to Message Queuing objects
* Authentication implemented through any authentication method supported by Windows 2000 including public key certificates, Kerberos or NTLM
* Encryption through both asymmetric (public/private keys) or symmetric (secret key) cryptography is used to encrypt messages sent between Message Queuing components
* Auditing to record which users attempt to access Message Queuing objects in Active Directory

Through the use of Message Queuing, no message loss and ensures that messages are not modified or viewed except by authorized users.
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-27-2008, 10:06 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Get all private and Public Message Queues in Local System:

Private Queues:

System.Messaging.MessageQueue[] privateMessageQueues = System.Messaging.MessageQueue.GetPrivateQueuesByMa chine("localhost");

Public Queues :

MessageQueueCriteria Crit = new MessageQueueCriteria();
Crit.MachineName = "localhost";
System.Messaging.MessageQueue[] publicMessageQueues = System.Messaging.MessageQueue.GetPublicQueues(Crit );
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 02-27-2008, 10:10 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Get All messages from Public and Private Queue :

Private Queue :

string queueName = "Private$\\test1";

System.Messaging.MessageQueue messageQ = new System.Messaging.MessageQueue(queueName);
System.Messaging.Message[] message = messageQ.GetAllMessages();


Public Queue :

string queueName = ".\\test1";

System.Messaging.MessageQueue messageQ = new System.Messaging.MessageQueue(queueName);
System.Messaging.Message[] message = messageQ.GetAllMessages();
__________________
S.Balasubramanian
Nothing is impossible

Last edited by Balasubramanian.S : 02-27-2008 at 10:21 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 02-27-2008, 10:16 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

when we receive a message from Message Queue using the following method, the received message is deleted from MessageQueue.

string message;
Object o = new Object();
System.Type[] arrTypes = new System.Type[2];
arrTypes[0] = typeof(string);
arrTypes[1] = o.GetType();
messageQ.Formatter = new XmlMessageFormatter(arrTypes);
message = ((string)messageQ.Receive().Body);
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 02-27-2008, 10:20 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

We can receive a message from Message Queue without deleting the message from the Queue using the following method.

string queueName = ".\\test1";

System.Messaging.MessageQueue msgQueue = new System.Messaging.MessageQueue(queueName);
{
Object o = new Object();
System.Type[] arrTypes = new System.Type[2];
arrTypes[0] = typeof(string);
arrTypes[1] = o.GetType();
msgQueue.Formatter = new XmlMessageFormatter(arrTypes);
System.Messaging.Message[] message = msgQueue.GetAllMessages();
if (message.Length > 0)
{
for (int i = 0; i < message.Length; i++)
{
string msg = (Payment)message[i].Body;
MessageBox.Show(msg, "Message Received!");
}
}
else
{
MessageBox.Show("There are no messages in this Queue");
}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 03-07-2008, 10:15 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Add messages to message Queue in Remote System :

1) Create a service that will run in the remote system.

2) This service contains the code to read or write the message queue.

3) Access this service from local system


Service to Add message in the Queue:

[WebMethod]
public string AddMessage(string queueName, string message)
{
string retValue = "";
try
{
if (!MessageQueue.Exists(queueName))
{
MessageQueue.Create(queueName);
}
System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(queueName);
msgQ.SetPermissions("administrator", MessageQueueAccessRights.FullControl);
Message msg = new Message();
msg.Body = message;
msgQ.Send(msg);
}
catch (Exception ex)
{
retValue = ex.ToString();
}
return retValue;
}
__________________
S.Balasubramanian
Nothing is impossible

Last edited by Balasubramanian.S : 03-07-2008 at 10:18 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 03-07-2008, 10:17 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Service to read messages from Message Queue from Remote System :

[WebMethod]
public string GetMessage(string queueName)
{
System.Messaging.MessageQueue messageQ = new System.Messaging.MessageQueue(queueName);
System.Type[] arrTypes = new System.Type[2];
Object o = new Object();
arrTypes[0] = typeof(string);
arrTypes[1] = o.GetType();
messageQ.Formatter = new XmlMessageFormatter(arrTypes);
string message = (string)messageQ.Receive().Body;
return message;
}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 03-11-2008, 10:16 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Application to Read messages from the service :

Add the above services as a web reference.

MSMQService.MSMQService msmqService = new MessageQueueServiceApplication.MSMQService.MSMQSer vice();
string message = msmqService.GetMessage(".\\test1");

test1 is the public queue. MSMQService is the webservice.
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 03-11-2008, 10:18 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Microsoft Messsage Queuing (MSMQ) in C#

Application to Add messages in the queue in Remote System using Web service :

Add the above service as a web reference.

MSMQService.MSMQService msmqService = new MessageQueueServiceApplication.MSMQService.MSMQSer vice();
string retValue = msmqService.AddMessage(".\\test1", "Hello");
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft Information Shanthi Technology BUZZzzzzz 5 03-21-2008 04:12 AM
Microsoft Message Queuing krishnakumar ASP and ASP.NET Programming 1 02-17-2008 10:01 PM
Microsoft delivered a new version of its Microsoft Security Assessment Tool (MSAT) senthilkannan Microsoft 0 12-19-2007 03:47 AM
Microsoft Windows XP SP3 senthilkannan Microsoft 2 11-27-2007 10:08 PM
Microsoft bit nhoj Microsoft 1 04-06-2007 02:03 AM


All times are GMT -7. The time now is 02:42 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0