Calling One-Way WCF Service using IOutputChannel

Note: This post is inspired by one of the chapter from 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

One way communication pattern allows client to send message to service and forget. Client doesn’t have to wait for the response.

One way WCF Service can be created as below,


using System.ServiceModel;

namespace WcfService1
{
[ServiceContract]
public interface IService1
{

[OperationContract(IsOneWay=true)]
void  InsertData();
}
}

What all you need to do is to pass parameter to Operation Contract as below,

clip_image001

Service is implemented as below,

namespace WcfService1
{

public class Service1 : IService1
{
public void InsertData()
{
//Insert here
}
}
}

 

In one way communication two interfaces are very important,

clip_image002

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

clip_image003

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  = new List<BindingElement>();
elements.Add(new OneWayBindingElement());
elements.AddRange(baseBinding.CreateBindingElements());
return new CustomBinding(elements);
}

&nbsp;

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

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

clip_image002[6]

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

image

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Client
{
class Program
{
static void Main(string[] args)
{
BasicHttpBinding binding = new BasicHttpBinding();
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("<a href="http://localhost:10828/Service1.svc">http://localhost:10828/Service1.svc</a>"));
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 below exception,

clip_image001[6]

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 Smile

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

2 responses to “Calling One-Way WCF Service using IOutputChannel”

  1. […] Calling One-Way WCF Service using IOutputChannel […]

  2. […] Calling One-Way WCF Service using IOutputChannel […]

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 )

Facebook photo

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

Connecting to %s

Create a website or blog at WordPress.com