In last post we discussed about SOAP Message Versions. Now let us move ahead and create a Message.
You can create a Message using CreateMessage() method of Message class . This method is overloaded with 11 different set of input parameters.
Easiest you can create a Message by just providing MessageVersion and action
On running above code snippet you will get as message as output and if you notice there is no body in the message since we have not added that.
If you want to add a body in the message you can do that but restriction is you can only add serialized object as message body.
Let us add a string to message body. Since string and other built in types are default serialized. So we do not need to explicitly serialize that.
In above function we have added a string message body with value “HI I am Body “. On running above code snippet you will get output as below,
Let us create a Message as object of custom class as message body. Assume you have a custom class called Product.
And you want to create a Message having instance of this class as message body. You can very much do that as below,
On running above code you will get output as below,
And you can see the custom object is being part of the Message there.
For your reference code to copy given below
Program.cs
using System; using System.ServiceModel.Channels; using System.ServiceModel; using System.Xml; using System.Collections.Generic; namespace XML { class Program { static void Main(string[] args) { Message msg = Message.CreateMessage(MessageVersion.Soap11, "urn:MyAction"); Console.WriteLine(msg.ToString()); Console.ReadKey(true); msg = Message.CreateMessage(MessageVersion.Soap11WSAddressing10, "urn:MyAction", "Hi I am Body"); Console.WriteLine(msg.ToString()); Console.ReadKey(true); Product p = new Product { ProductId = 1, ProductName = "Books", ProductPrice = 110.0 }; msg = Message.CreateMessage(MessageVersion.Soap12, "urn: MY Action", p); Console.WriteLine(msg.ToString()); Console.ReadKey(true); } } public class Product { public int ProductId { get; set; } public string ProductName { get; set; } public double ProductPrice { get; set; } } }
Now you know how you could create a Message in WCF. I hope this post was useful. Thanks for Reading
Follow @debugmode_
Leave a Reply