wcf
  1. wcf-duplex-message-exchange

Duplex Message Exchange - (WCF)

In WCF (Windows Communication Foundation), duplex message exchange is a communication pattern where the client can send and receive messages from the service simultaneously. This pattern is commonly used in scenarios where the service needs to provide real-time updates or notifications to the client.

Syntax

To implement duplex message exchange in WCF, you will need to define a callback contract in your service interface that the client can use to receive messages. Here's an example callback contract:

[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public interface IMyService
{
    [OperationContract]
    void DoWork();
}

public interface IMyServiceCallback
{
    [OperationContract]
    void Notify(string message);
}

To enable duplex message exchange, the client must first create an instance of the callback contract and pass it to the service when it calls the service operation. Here's an example client code:

// create callback instance
var callback = new MyServiceCallback();

// create service proxy
var factory = new DuplexChannelFactory<IMyService>(callback);
var service = factory.CreateChannel();

// call service operation
service.DoWork();

In the service implementation, you can use the callback contract to send messages to the client. Here's an example service code:

public class MyService : IMyService
{
    public void DoWork()
    {
        // get callback channel for client
        var callback = OperationContext.Current.GetCallbackChannel<IMyServiceCallback>();

        // send a message to the client
        callback.Notify("Work done!");
    }
}

Example

Here's an example of how to implement duplex message exchange in WCF using C#:

[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public interface IMyService
{
    [OperationContract]
    void DoWork();
}

public interface IMyServiceCallback
{
    [OperationContract]
    void Notify(string message);
}

public class MyService : IMyService
{
    public void DoWork() 
    {
        // get callback channel for client
        var callback = OperationContext.Current.GetCallbackChannel<IMyServiceCallback>();

        // send a message to the client
        callback.Notify("Work done!");
    }
}

public class MyServiceCallback : IMyServiceCallback
{
    public void Notify(string message) 
    {
        Console.WriteLine("Notification: " + message);
    }
}

class Program
{
    static void Main(string[] args)
    {
        // create callback instance
        var callback = new MyServiceCallback();

        // create service proxy
        var factory = new DuplexChannelFactory<IMyService>(callback);
        var service = factory.CreateChannel();

        // call service operation
        service.DoWork();

        Console.ReadLine();
    }
}

Output

When you run the client code, you should see output that looks like this:

Notification: Work done!

Explanation

In the example code, we define a service interface (IMyService) that includes a service operation (DoWork) and a callback contract (IMyServiceCallback) that includes a callback operation (Notify). We implement the service logic in the MyService class, where we use the OperationContext.Current.GetCallbackChannel method to get the callback channel for the client and send a message to the client using the Notify callback operation. In the client code, we create an instance of the MyServiceCallback class to handle the callback messages, create a service proxy using the DuplexChannelFactory class, and call the DoWork service operation. When the service sends a message back to the client, the Notify callback operation is called and the message is printed to the console.

Use

Duplex message exchange is commonly used in WCF applications for real-time updates and notifications. It is especially useful in scenarios where the client needs to receive updates from the service without explicitly requesting them.

Important Points

  • Duplex message exchange is a communication pattern in WCF where the client can send and receive messages from the service simultaneously.
  • To enable duplex message exchange, you will need to define a callback contract in your service interface, create a callback instance in your client code, and call the service operation using a duplex channel factory.
  • The OperationContext.Current.GetCallbackChannel method can be used to get the callback channel for the client in the service implementation.

Summary

In this page, we discussed how to implement duplex message exchange in WCF using C#. We covered the syntax, example, output, explanation, use, important points, and summary of duplex message exchange in WCF. Duplex message exchange is a powerful communication pattern that enables real-time updates and notifications in WCF applications.

Published on: