|
Cannot Cast MSMQ Message Body Back To Original Class -- slingblade --
Hi, I am working on an MSMQ and I am passing a custom object through the queue to a different application. My problem is once I recieve the message from the queue I cannot cast it from message.body back into its custom class. Here is the jest of the code: <sending the message> Message myMessage = Message( customClass, new BinaryMessageFormatter() ); myQueue.Send(myMessage); <recieving the message> System.Messaging.Message message = messageQueue.EndReceive (e.AsyncResult); customClass myCustomClass = ( customClass )message.Body; => Error here At this point I recieve an invalid cast exception. I have used the GetType method on the message body to check the type of the object and it says it is <i>exactly</i> the type of object I am trying to cast to. <GetType Code> object TestType = message.Body.GetType().ToString(); (TestType == customClass) I have copied and pasted the custom class from one application to the other to make sure they are exacly the same. I am at a loss to figure out what I am doing wrong. Any ideas? |
|
-- slingblade --
I found a solution, please let me know if there was an easier one. What I found was that even though the code was the same in both classes in both assemblies, the clr didn't recognise them as being the same class. Looking back this makes perfect sense. To bypass this problem I created an assemble and in it I created my custom object. I then deleted the custom object from my both applications and instead I referenced this new assembly in both to get to the custom object and its properties/methods. This way both applications share the exact same class object. Now I can cast the message body to the custom object, no problem. sling blade wrote: Hi, I am working on an MSMQ and I am passing a custom object through the queue to a different application. My problem is once I recieve the message from the queue I cannot cast it from message.body back into its custom class. Here is the jest of the code: <sending the message> Message myMessage = Message( customClass, new BinaryMessageFormatter() ); myQueue.Send(myMessage); <recieving the message> System.Messaging.Message message = messageQueue.EndReceive (e.AsyncResult); customClass myCustomClass = ( customClass )message.Body; => Error here At this point I recieve an invalid cast exception. I have used the GetType method on the message body to check the type of the object and it says it is <i>exactly</i> the type of object I am trying to cast to. <GetType Code> object TestType = message.Body.GetType().ToString(); (TestType == customClass) I have copied and pasted the custom class from one application to the other to make sure they are exacly the same. I am at a loss to figure out what I am doing wrong. Any ideas? |