I’m having a frustrating time with C# message serialization.
I have a class which has a constructor which looks like this:
public ProposalRequestMessage(int imaNumber, int proposalId, bool trainingFlag, string firstSiteAddress,
bool lastSiteFlag, string lastSiteAddress, int reasonCode,
List<LaneSelection> theLaneSelections)
{
ImaNumber = imaNumber;
ProposalId = proposalId;
TrainingFlag = trainingFlag;
FirstSiteAddress = firstSiteAddress;
LastSiteFlag = lastSiteFlag;
LastSiteAddress = lastSiteAddress;
ReasonCode = reasonCode;
laneSelections = new List<LaneSelection>(theLaneSelections);
}
The lanesSelections member of the class of of type System.Collections.Generic.List, where a LaneSelection looks like this:
public class LaneSelection
{
public int LaneId { get; set; }
public SignalAspect AspectCode { get; set; }
public LaneSelection()
{
}
public LaneSelection(int laneId, SignalAspect aspectCode)
{
LaneId = laneId;
AspectCode = aspectCode;
}
}
A SignalAspect is an enumeration.
I send a message containing an instance of this class over an MSMQ as follows:
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(queuename);
queue.Purge();
System.Messaging.Message msg = new System.Messaging.Message(theMessage, new System.Messaging.XmlMessageFormatter());
queue.Send(msg);
Using some debug tools, I have found that the resulting XML looks a bit like this:
<?xml version="1.0"?>
<IvtmMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MessageType>ProposalRequest</MessageType>
<ProposalRequestMessage>
<ImaNumber>0</ImaNumber>
<ProposalId>2</ProposalId>
<TrainingFlag>false</TrainingFlag>
<FirstSiteAddress>M25/4690A</FirstSiteAddress>
<LastSiteFlag>false</LastSiteFlag>
<LastSiteAddress />
<ReasonCode>3</ReasonCode>
<LaneSelections>
<LaneSelection>
<LaneId>1</LaneId>
<AspectCode>Advisory20</AspectCode>
</LaneSelection>
</LaneSelections>
</ProposalRequestMessage>
I deserialize the message at the other end like so:
Queue = new System.Messaging.MessageQueue(queueName);
Queue.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(IvtmMessage) });
Queue.ReceiveCompleted += new System.Messaging.ReceiveCompletedEventHandler(Queue_ReceiveCompleted);
Queue.BeginReceive(new System.TimeSpan(0, 0, 0, 30));
...
System.Messaging.MessageQueue mq = (System.Messaging.MessageQueue)sender;
try
{
// End the asynchronous Receive operation.
System.Messaging.Message m = mq.EndReceive(e.AsyncResult);
IvtmMessage message = (IvtmMessage)m.Body;
DecodeMessage(message);
}
catch (System.Messaging.MessageQueueException ex)
{
string exception = ex.Message;
}
mq.BeginReceive();
return;
Every member of the class is correctly deserialized except for the laneSelections element which, although it clearly has a value in the XML, evaluates to a null instances in the deserialized message.
In deseparation I tried adding a List to the class, populating it with the values 1-5 on construction. If this serialized correctly then it would show me that the problem is with the LaneSelection class, but if not then the issue would be with serializing a List. The List did not serialize correctly.
Does anyone know what’s going wrong?
two things;
Property definition;
Without the proper Xml attributes on the properties and classes you’re leaving it up to the serializer to interpret how the properties/objects should be translated