WCF Proxy usage and Message styling

Damir Dobric Posts

Next talks:



Follow me on Twitter: #ddobric




WCF tools like SVCUTIL builds powerful proxy classes which hide all messaging details from developer. In this post I would like to show step by step three variants of invoking of one service operation.

  1. When you have a proxy class:
    The first one is very simple one which shows how to invoke the operation when you have the proxy class build with SVCUTIL tool. This sample assumes that the configuration is in the app.config file.
  2. When you have interface only
    This example shows how to invoke the operation when you have the interface class (mostly just copied from the service code). This example also assumes that the configuration is in the app.config file.
  3. When you have nothing
    This example shows how to invoke the operation without of ServiceModel. That means you have to build the message, create the right native channel and parse the result.

All examples in this post are based on following contract:  

System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "")]

[System.ServiceModel.ServiceContractAttribute(Namespace="http://daenet.eu/servicebus", ConfigurationName="ServiceBus")]

public interface ServiceBus



    [System.ServiceModel.OperationContractAttribute(Action="http://daenet.eu/servicebus/Send", ReplyAction="http://daenet.eu/servicebus/ServiceBus/SendResponse")]

    int Send(string par1, string par2, string par3, string par4, string par5);



Example 1: When you have a proxy class:

public void When_I_have_Proxy()


    ServiceBusClient proxy = new ServiceBusClient();

    proxy.Send("msg", "id", "msgversion", "action", "topic");


Example 2: When you have interface only:



    public void When_I_have_Interface()


        ChannelFactory<ServiceBus> factory = new ChannelFactory<ServiceBus>("BasicHttpBinding_ServiceBus");

        ServiceBus proxy = factory.CreateChannel();

        proxy.Send("requestMessage", "idmessageId", "version", "action", "topic");


Example 3: When you have nothing (no service model tooling):


    public void When_I_have_Nothing()


        // Create factory.

        ChannelFactory<IRequestChannel> factory = new ChannelFactory<IRequestChannel>(new BasicHttpBinding(),



        // Creates the native channel.

        IRequestChannel requestChannel = factory.CreateChannel();



        // Creates the body writer, which will build the message for you.

        // This is what ServiceModel does.

        DynamicBodyWriter bodyWriter = new DynamicBodyWriter("Send",

            new string[] {"requestMessage", "idmessageId", "version", "action", "topic"},

            new object[] { "first parameter as string", "second param as string",

                "third param as string", "fourth param", "last param" },




        // Creeates the message.

        // More about message creation: http://developers.de/blogs/damir_dobric/archive/2006/07/16/746.aspx

        Message msg = Message.CreateMessage(MessageVersion.Soap11,




        // Invokes the operation.

        Message response = requestChannel.Request(msg);



        // Shows how to parse the result contained in the response message.

        using (XmlDictionaryReader reader = response.GetReaderAtBodyContents())


            string a = (string)reader.ReadContentAs(typeof(string), null);



Following code shows the full implementation of the DynamicBodyWriter class, which has been used in the previous example:

using System;

using System.Linq;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel.Channels;

using System.Xml;


namespace Daenet.MobileServiceBus.Demo


    internal class DynamicBodyWriter : BodyWriter


        private string op;

        private string[] varnames;

        private object[] varvals;

        private string ns;


        /// <summary>

        /// Creates the helper class for building of messages.

        /// </summary>

        /// <param name="op">The name of the service operation to be invoked.</param>

        /// <param name="varnames">The list of parameters of invoking operation.</param>

        /// <param name="varvals">The list of values.</param>

        /// <param name="ns">The namespace. I.e.: http://daenet.eu/servicebus</param>

        public DynamicBodyWriter(string op, string[] varnames, object[] varvals, string ns)

            : base(true)


            this.op = op;

            this.varnames = varnames;

            this.varvals = varvals;

            this.ns = ns;



        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)


            writer.WriteStartElement(op, ns);

            for (int i = 0; i < varnames.Length; i++)

                writer.WriteElementString(varnamesIdea, varvalsIdea.ToString());





Posted Sep 09 2007, 08:28 PM by Damir Dobric
Filed under:


Damir Dobric Posts wrote Mobility Day Zagreb 2007
on 09-10-2007 9:02

At Sept. 11. 2007 I will talk at Mobility Day in Zagreb about new communication possibilities around

kqclaefclsr wrote re: WCF Proxy usage and Message styling
on 09-13-2012 22:02

E2uDZT  <a href="swgrcnxjxbhp.com/.../a>, [url=http://zofjzezyeria.com/]zofjzezyeria[/url], [link=http://mfsozzomalyt.com/]mfsozzomalyt[/link], http://jjvsbnupcewb.com/

developers.de is a .Net Community Blog powered by daenet GmbH.