简体   繁体   中英

How to consume MSMQ service

I want to consume msmq service. But unable to send message to queue. Here is my code.

System.Messaging.MessageQueue msmQ = new System.Messaging.MessageQueue("net.msmq://myServerName/private/MyQueueName");

msg ="<nodeDetails><node>Node1</node></nodeDetails>";//Dummy value. it is XML structure consist of multiple node
 msmQ.Send(msg);

It give me error on msmQ.Send(msg)

error Message ="Length cannot be less than zero. Parameter name: length"

Following things are install on my m/c

  1. Microsoft Message Queue(MSMQ)Server
  2. Window Activation Process

Also when I tried as

 bool msmQExits = MessageQueue.Exists("net.msmq://myServerName/private/MyQueueName");

But it gives "Path syntax is invalid"

I am not able to get anything on it. What I have is just a msmq URL "net.msmq://myServerName/private/MyQueueName"

Please let me know how can I consume such url and Send my message to "MyQueueName"

Any help is appreciated

Thanks in advanced.

Change your queue name to this:

var queueName = @"FormatName:DIRECT=HTTP://URLAddressSpecification/net.msmq://myServerName/private/MyQueueName";

And you cannot check if a remote query exists by MessageQueue.Exists method. It will always throw an exception.

You can check these links for more info:

Also, the problem is not with the message you see that length is less than 0. If you go deeper and check the stack trace you'll see that your queue name has an invalid format. It tries to find FORMAT occurrence inside your queue name, doesn't find it and Substring() method returns -1 there.

Stacktrace:

   at System.String.Substring(Int32 startIndex, Int32 length)
   at System.Messaging.MessageQueue.ResolveFormatNameFromQueuePath(String queuePath, Boolean throwException)
   at System.Messaging.MessageQueue.get_FormatName()
   at System.Messaging.MessageQueue.SendInternal(Object obj, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType)
   at System.Messaging.MessageQueue.Send(Object obj)
   at MessagingTest.Program.SendMessage(String str, Int32 x) in c:\Users\ivan.yurchenko\Documents\Visual Studio 2013\Projects\MSMQTestProjects\MessagingTest\MessagingTest\Program.cs:line 21
   at MessagingTest.Program.<Main>b__1() in c:\Users\ivan.yurchenko\Documents\Visual Studio 2013\Projects\MSMQTestProjects\MessagingTest\MessagingTest\Program.cs:line 38
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()

Here is an example for how to consume the service.

It has Wcf Service,Physical MSMQ, and the client project. So you have to have a WCF service to receive the message and msmq to store the message and a client to send the message.

http://www.codeproject.com/Articles/326909/Creating-a-WCF-Service-with-MSMQ-Communication-and

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