Create Message in WCF

In last post  we discussed about SOAP Message Versions. Now let us move ahead and create a Message.

image

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

image

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.

image

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.

image

 

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,

image

 

Let us create a Message as object of custom class as message body. Assume you have a custom class called Product.

image

And you want to create a Message having instance of this class as message body. You can very much do that as below,

image

On running above code you will get output as below,

image

And you can see the custom object is being part of the Message there.

For your reference code to copy given below Smile

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

3 thoughts on “Create Message in WCF

  1. Pingback: Distributed Weekly 107 — Scott Banwart's Blog

  2. 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.

  3. Pingback: Monthly Report June 2011: Total Posts 33 « debug mode……

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s