MSMQ - Specified cast is not valid Error

S

SRLoka

I have an application that has five threads. All five threads write to the
same MSMQ. All five threads are using the same global instance to send data
to the MSMQ server(on same machine).
Occasionally, I get the erroror below. The data(string) being sent is not
null or empty. Is this a problem related to multiple threads ? Right now I
have coded it like this

Controller.MSMQ.Send(Record);

Should I change to

lock(Controller.MSMQ)
{
Controller.MSMQ.Send(Record);
}

Thanks.
Below is the actual error

Specified cast is not valid.
P R O P E R T I E S
------------------
Message: Specified cast is not valid.
InnerException:
TargetSite: MQPROPS Lock()
StackTrace: at System.Messaging.Interop.MessagePropertyVariants.Lock()
at System.Messaging.Message.Lock()
at System.Messaging.MessageQueue.SendInternal(Object obj,
MessageQueueTransaction internalTransaction, MessageQueueTransactionType
transactionType)
at System.Messaging.MessageQueue.Send(Object obj)
at TCPServer.CH.Process(String ThreadName)
HelpLink:
Source: System.Messaging
 
S

SRLoka

Or should I use a separate instance for each thread ?
(Always too quick to hit that send)

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

SRLoka,

I would definitely create a separate instance for each thread that you
are using. The documentation for the class states that the following
methods are thread-safe:

BeginPeek, BeginReceive, EndPeek, EndReceive, GetAllMessages, and Peek,
Receive.

Unfortunately, you are using the Send message, which is not thread-safe.
I would start with using separate instances on each thread.

Also, what formatter are you using to send the message? I ask only
because some formatters require the type to be serializable.

Hope this helps.
 
S

SRLoka

Thanks Nicholas. The Record variable is just a string.
Would it be OK to use a Lock
lock(MSMQ)
{
MSMQ.Send
}
instead of a separate queue opened for each thread ?
Will a separate queue for each thread be more efficient. ? Looks like it
based on the docs on MSDN. I will try the lock first(easy way out) and then
code for the separate queue.

Thanks



Nicholas Paldino said:
SRLoka,

I would definitely create a separate instance for each thread that you
are using. The documentation for the class states that the following
methods are thread-safe:

BeginPeek, BeginReceive, EndPeek, EndReceive, GetAllMessages, and Peek,
Receive.

Unfortunately, you are using the Send message, which is not
thread-safe. I would start with using separate instances on each thread.

Also, what formatter are you using to send the message? I ask only
because some formatters require the type to be serializable.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SRLoka said:
Or should I use a separate instance for each thread ?
(Always too quick to hit that send)

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

I would use a separate instance on each thread. After all, the MSMQ
subsystem knows how to handle messages coming in on different threads.
There is no need for you to try and second-guess it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SRLoka said:
Thanks Nicholas. The Record variable is just a string.
Would it be OK to use a Lock
lock(MSMQ)
{
MSMQ.Send
}
instead of a separate queue opened for each thread ?
Will a separate queue for each thread be more efficient. ? Looks like it
based on the docs on MSDN. I will try the lock first(easy way out) and
then code for the separate queue.

Thanks



Nicholas Paldino said:
SRLoka,

I would definitely create a separate instance for each thread that you
are using. The documentation for the class states that the following
methods are thread-safe:

BeginPeek, BeginReceive, EndPeek, EndReceive, GetAllMessages, and Peek,
Receive.

Unfortunately, you are using the Send message, which is not
thread-safe. I would start with using separate instances on each thread.

Also, what formatter are you using to send the message? I ask only
because some formatters require the type to be serializable.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SRLoka said:
Or should I use a separate instance for each thread ?
(Always too quick to hit that send)

Thanks
 

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