Duplex Service

There are 3 message exchange patterns :

1. Request Response 2.One way  3.Duplex

A duplex service contract is a message exchange pattern in which both endpoints can send messages to the other independently. A duplex service, therefore, can send messages back to the client endpoint, providing event-like behavior. Duplex communication occurs when a client connects to a service and provides the service with a channel on which the service can send messages back to the client. Note that the event-like behavior of duplex services only works within a session.

To create a duplex contract you create a pair of interfaces. The first is the service contract interface that describes the operations that a client can invoke. That service contract must specify a callback contract in the System.ServiceModel.ServiceContractAttribute.CallbackContract property. The callback contract is the interface that defines the operations that the service can call on the client endpoint. A duplex contract does not require a session, although the system-provided duplex bindings make use of them.

In IService Interface :

[ServiceContract(CallbackContract=typeof(ReturnData),SessionMode=SessionMode.Required)]
public interface IService1
{
[OperationContract(IsOneWay=true)]
void sendData(string num1);
}

[ServiceContract]
public interface ReturnData
{
[OperationContract(IsOneWay=true)]
void reData(string num2);
}

In Service Class :

[ServiceBehavior(InstanceContextMode= InstanceContextMode.PerSession)]
public class Service1 : IService1
{
ReturnData rd = OperationContext.Current.GetCallbackChannel<ReturnData>();

public void sendData(string num1)
{
num1 = num1 + “pass to ReturnData “;
rd.reData(num1);
}
}

On the client :

static void Main(string[] args)
{
InstanceContext contexobj = new InstanceContext(new ClientCall());

ServiceReference1.Service1Client obj = new ConsoleClient.ServiceReference1.Service1Client(contexobj);

Console.WriteLine(“sending data”);

obj.sendData(“hi..”);
Console.ReadLine();
}

Also create one class called ClientCall.cs

class ClientCall:ServiceReference1.IService1Callback
{
public void reData(string num2)
{
Console.WriteLine(“data : ” + num2);
}
}

 

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