Create Message in WCF

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 Smile


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");

            msg = Message.CreateMessage(MessageVersion.Soap11WSAddressing10,
                                         "Hi I am Body");


            Product p = new Product
                ProductId = 1,
                ProductName = "Books",
                ProductPrice = 110.0
            msg = Message.CreateMessage(MessageVersion.Soap12,
                                        "urn: MY Action",



    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

3 thoughts on “Create Message in WCF

  1. Great article. Nice and simple!

    I have some problems with doing my own custom message creation. By writing similar code into an implementation of IClientMessageFormatter, I am getting a SOAP message that is not accepted by the mock service I created in “SOAP UI”. I am getting an “Missing operation for soapAction “-error.

    As this is the main difference, I am suspecting that the cause is that the default xml namespaces are defined in the node element just below the s:Body-element (i.e. the object inserted into the envelope). In the message generated by default (which is accepted by the mock service) these are defined in the s:Body element. However, I have no idea how to get my message created in such a way that these default xml namespaces are defined on the s:Body level.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s