---
title: "How does WCF support duplex communication?"  
description: "How does WCF support duplex communication?"  
author: "ICSM Computer"  
published: 2025-05-28  
updated: 2025-05-28  
canonical: https://www.mindstick.com/interview/34176/how-does-wcf-support-duplex-communication  
category: "c#"  
tags: ["c#", "wcf"]  
reading_time: 4 minutes  

---

# How does WCF support duplex communication?

WCF supports **duplex communication** using a **callback contract**, enabling two-way communication between a service and its client. This is useful for scenarios like chat applications, real-time notifications, or event-based systems.

## How Duplex Communication Works in WCF

1. **Client calls the service**
2. **Service holds a reference to the client’s callback**
3. **Service calls back the client asynchronously**

## Key Components

| Component | Description |
| --- | --- |
| `ServiceContract` | The main service interface |
| `CallbackContract` | An interface that defines operations the **client must implement** |
| `InstanceContextMode` | Usually set to `PerSession` to maintain the callback reference |
| `binding` | Must support sessions (e.g., `NetTcpBinding`, `WSDualHttpBinding`) |

## Example

### 1. Define Callback Contract

```cs
public interface IMyCallback
{
    [OperationContract(IsOneWay = true)]
    void OnNotify(string message);
}
```

### 2. Define Service Contract with Callback

```cs
[ServiceContract(CallbackContract = typeof(IMyCallback))]
public interface IMyService
{
    [OperationContract]
    void Register();

    [OperationContract]
    void SendMessage(string msg);
}
```

### 3. Implement the Service

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService
{
    public void Register()
    {
        var callback = OperationContext.Current.GetCallbackChannel<IMyCallback>();
        callback.OnNotify("Registered successfully.");
    }

    public void SendMessage(string msg)
    {
        var callback = OperationContext.Current.GetCallbackChannel<IMyCallback>();
        callback.OnNotify("Server received: " + msg);
    }
}
```

### 4. Create Client with Callback Implementation

```cs
public class MyClientCallback : IMyCallback
{
    public void OnNotify(string message)
    {
        Console.WriteLine("Received from server: " + message);
    }
}
```

### 5. Connect Client

```cs
var instanceContext = new InstanceContext(new MyClientCallback());
var client = new MyServiceClient(instanceContext); // auto-generated proxy
client.Register();
client.SendMessage("Hello from client");
```

## Required Bindings for Duplex

| Binding | Supports Duplex? | Notes |
| --- | --- | --- |
| `NetTcpBinding` | Yes | Best for intranet scenarios |
| `WSDualHttpBinding` | Yes | Allows duplex over HTTP (complex setup) |
| `NetNamedPipeBinding` | Yes | For on-machine communication |
| `BasicHttpBinding` | NO | No duplex support |

## Answers

### Answer by ICSM Computer

WCF supports **duplex communication** using a **callback contract**, enabling two-way communication between a service and its client. This is useful for scenarios like chat applications, real-time notifications, or event-based systems.

## How Duplex Communication Works in WCF

1. **Client calls the service**
2. **Service holds a reference to the client’s callback**
3. **Service calls back the client asynchronously**

## Key Components

| Component | Description |
| --- | --- |
| `ServiceContract` | The main service interface |
| `CallbackContract` | An interface that defines operations the **client must implement** |
| `InstanceContextMode` | Usually set to `PerSession` to maintain the callback reference |
| `binding` | Must support sessions (e.g., `NetTcpBinding`, `WSDualHttpBinding`) |

## Example

### 1. Define Callback Contract

```cs
public interface IMyCallback
{
    [OperationContract(IsOneWay = true)]
    void OnNotify(string message);
}
```

### 2. Define Service Contract with Callback

```cs
[ServiceContract(CallbackContract = typeof(IMyCallback))]
public interface IMyService
{
    [OperationContract]
    void Register();

    [OperationContract]
    void SendMessage(string msg);
}
```

### 3. Implement the Service

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService
{
    public void Register()
    {
        var callback = OperationContext.Current.GetCallbackChannel<IMyCallback>();
        callback.OnNotify("Registered successfully.");
    }

    public void SendMessage(string msg)
    {
        var callback = OperationContext.Current.GetCallbackChannel<IMyCallback>();
        callback.OnNotify("Server received: " + msg);
    }
}
```

### 4. Create Client with Callback Implementation

```cs
public class MyClientCallback : IMyCallback
{
    public void OnNotify(string message)
    {
        Console.WriteLine("Received from server: " + message);
    }
}
```

### 5. Connect Client

```cs
var instanceContext = new InstanceContext(new MyClientCallback());
var client = new MyServiceClient(instanceContext); // auto-generated proxy
client.Register();
client.SendMessage("Hello from client");
```

## Required Bindings for Duplex

| Binding | Supports Duplex? | Notes |
| --- | --- | --- |
| `NetTcpBinding` | Yes | Best for intranet scenarios |
| `WSDualHttpBinding` | Yes | Allows duplex over HTTP (complex setup) |
| `NetNamedPipeBinding` | Yes | For on-machine communication |
| `BasicHttpBinding` | NO | No duplex support |


---

Original Source: https://www.mindstick.com/interview/34176/how-does-wcf-support-duplex-communication

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
