Calling One-Way WCF Service using IOutputChannel


Note: This post is inspired by one of the chapters from the book Essential WCF. I tried to put the content in my way. Thanks to the original author. I strongly recommend you to read this post http://winterdom.com/2007/08/ioutputchanneloverirequestchannel

A one way communication pattern allows clients to send messages to the service and forget. The client doesn't have to wait for the response.

A one way WCF Service can be created as below,

using System.ServiceModel;

namespace WcfService1
{  
    [ServiceContract]
   
publicinterfaceIService1
    {
        [OperationContract(IsOneWay=true)]
       
void  InsertData();     
    } 
}


All you need to do is to pass a parameter to Operation Contract as below,

WCF1.gif

Service is implemented as below,

namespace WcfService1
{
   
publicclassService1 : IService1

    {
       public void
InsertData()
        {
            
//Insert here
        }     
    }
}


In one way communication, two interfaces are very important,

WCF2.gif

These two interfaces are used to send messages between the service and client in a one way communication pattern.

WCF3.gif

At the service side we are using basicHttpBinding and this does not support one-way communication. It is made for Request-reply communication pattern. So you may consider using custom binding made on stack of basicHttpBinding.

static
CustomBinding CreateOneWayBinding(Binding baseBinding)

{
     List<BindingElement> elements = newList<BindingElement>();
  
  elements.Add(newOneWayBindingElement());
     elements.AddRange(baseBinding.CreateBindingElements());
     returnnewCustomBinding(elements)

}

The above function can be used at the WCF client side as well to create custom binding supporting one-way communication.

At client side we can call One-Way WCF Services using IOutputChannel. For that you need to add the below references to the client project.

WCF4.gif

You can create client side one-way communication supporting custom binding as below,

WCF5.gif

using
System.Collections.Generic;

using System.Linq;
using
System.Text;
using
System.ServiceModel;
using
System.ServiceModel.Channels;
using
WPFTileGame; 

namespace
Client
{

    class
Program

    {

        static void Main(string[] args)

        {

            BasicHttpBinding binding = newBasicHttpBinding();

            CustomBinding bindingToprfoemOneWay = CreateOneWayBinding(binding);

            BindingParameterCollection parameters = new BindingParameterCollection();

            Message message = Message.CreateMessage(MessageVersion.Soap11, "urn:sendmessage");

            IChannelFactory<IOutputChannel> factory = bindingToprfoemOneWay.BuildChannelFactory<IOutputChannel>(parameters);

            factory.Open();

            IOutputChannel channel = factory.CreateChannel(new EndpointAddress("http://localhost:10828/Service1.svc"));

            channel.Open();

            channel.Send(message);

            channel.Close();

            factory.Close();

            Console.ReadKey();

        }

    }
}

If you are not using custom binding at service side and relying on basicHttpBinding then very likely you will get the below exception,

WCF6.gif

In this way you can make one-way communication call to WCF Service from the client.

I hope this post was useful. Thanks for reading.

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you

Up Next
    Ebook Download
    View all
    Learn
    View all