none
msmq serialization RRS feed

  • Question

  • Hello all,

    I have a (VB) interface that is constantly polling an instrument for readings and saving them in a database.

    I am adding a (C#) service so it can save readings even when the computer is logged off, so I'm implementing a message queue so the two applications can talk to each other.

    The problem I'm getting is that when the interface sends the service a message, the service reports "'message.Body' threw an exception of type 'System.InvalidOperationException'" and "Cannot deserialize the message passed as an argument. Cannot recognize the serialization format."

    Interface:

        <Serializable()> _
        Structure Communique
            Dim CommPort, Command, Pattern As String
            Dim TimeOutSeconds As Int32
    
            Public Sub New(ByVal CommPort As String, ByVal Command As String, ByVal Pattern As String, ByVal TimeOutSeconds As Int32)
                Me.CommPort = CommPort
                Me.Command = Command
                Me.Pattern = Pattern
                Me.TimeOutSeconds = TimeOutSeconds
            End Sub
        End Structure
    
    .
    .
    .
    
    Try
                'If we don't recreate the MessageQueue every time, we get the error "Format name is invalid" (Why????????)
                If Not MessageQueue.Exists(".\Private$\Serial") Then
    
                    msgQ = MessageQueue.Create(".\Private$\Serial")
    
                Else
    
                    msgQ = New MessageQueue(".\Private$\Serial")
    
                End If
    
                Dim msg = New System.Messaging.Message() With {.Body = New Communique(CommPort, Command, Pattern, TimeOutSeconds)}
                msgQ.Send(msg)
    
            Catch ex As Exception
    
                Throw ex
    
            End Try

    Service:

            [Serializable]
            public struct Communique
            {
                string CommPort, Command, Pattern;
                int TimeOutSeconds;
            }
    
    .
    .
    .
            private void OnReceiveCompleted(Object source,
              ReceiveCompletedEventArgs asyncResult)
            {
                try
                {
    
                    MessageQueue mq = (MessageQueue)source;
    
                    if (mq != null)
                    {
                        try
                        {
                            System.Messaging.Message message = null;
                            try
                            {
                                message = mq.EndReceive(asyncResult.AsyncResult);
                            }
                            catch (Exception ex)
                            {
                                LogEvent(ex.Message);
                            }
                            if (message != null)
                            {
    
                                //Here's where I get the error!!!
                                if (message.Body is Communique)
                                {
                                    receivedCounter++;
                                    if ((receivedCounter % 10000) == 0)
                                    {
                                        string messageText =
                                            string.Format("Received {0} messages", receivedCounter);
                                        LogEvent(messageText);
                                    }
                                }
                            }
                        }
                        finally
                        {
                            if (isRunning)
                            {
                                mq.BeginReceive();
                            }
                        }
                    }
                    return;
                }
                catch (Exception exc)
                {
                    LogEvent(exc.Message);
                }
            }


    Thank you in advance.


    • Edited by trufaux Wednesday, October 22, 2014 3:11 PM changed "message" to "service"
    Wednesday, October 22, 2014 3:10 PM

Answers

  • You need to make sure you are using the exact same serialization format on sending and receiving side (especially considering text Encodings). This can be done by defining the Formatter property of the Message instance before setting or reading its Body property. Or you take over serialisation and deserialisation yourself and use the BodyStream property of the Message instead of the Body property.
    Friday, October 24, 2014 1:36 PM
  • Thanks.  I had to set the formatter to XMLMessageFormatter and specify the targetTypes on both sides.  Works great!
    Wednesday, October 29, 2014 9:03 PM

All replies