static Delivery SendDataContact() { // create a serializer, it is useful to make this a static object in your class because // you will use it over and over and over DataContractSerializer serializer = new DataContractSerializer(typeof(MyContract)); // create an object of your contact MyContract data = new MyContract(); data.DataItem1 = "Item1"; data.DataItem2 = 2.0; data.DataItem3 = 3; // create a memory stream to load the XML object into MemoryStream stream = new MemoryStream(); // serilize the object to XML serializer.WriteObject(stream, data); // put the message into a delivery for sending Delivery msg = new Delivery(stream.ToArray(), DeliveryEncoding.Utf8); // send the message return(msg); }
static void ReceiveDataContract(Delivery msg) { // create a serializer, it is useful to make this a static object in your class because // you will use it over and over and over DataContractSerializer serializer = new DataContractSerializer(typeof(MyContract)); // get the binary structure in the delivery byte[] msgData = msg.Message(); // put the structure in a stream MemoryStream stream = new MemoryStream(msgData); stream.Position = 0; // deserialize the message MyContract data = (MyContract)serializer.ReadObject(stream); // access the object same as it was sent string val1 = data.DataItem1; double val2 = data.DataItem2; int val3 = data.DataItem3; }