---
title: "What are the different instance management modes available in WCF?"  
description: "What are the different instance management modes available in WCF?"  
author: "ICSM Computer"  
published: 2025-05-27  
updated: 2025-05-27  
canonical: https://www.mindstick.com/interview/34169/what-are-the-different-instance-management-modes-available-in-wcf  
category: "c#"  
tags: ["c#", "wcf"]  
reading_time: 4 minutes  

---

# What are the different instance management modes available in WCF?

In **WCF (Windows Communication Foundation)**, **instance management** defines **how service objects are created and maintained** for client requests. WCF provides three main **instance context modes**, configured using the `[ServiceBehavior]` attribute's `InstanceContextMode` property.

### 1. PerCall (default)

- **A new service instance is created for each client request.**
- No state is maintained between calls.
- Ideal for **stateless services**.

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService
{
    // A new instance is created per request
}
```

1. **Pros:**

   1. High scalability.
   2. No need for synchronization.
   3. Low memory usage.

2. **Cons:**

   1. Cannot maintain service-level state.

### 2. PerSession

- **One service instance is created per client session** (requires session-enabled binding like `NetTcpBinding` or `WSHttpBinding` with reliable session).
- State is preserved throughout the session.

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService
{
    // Maintains state per client session
}
```

1. **Pros:**

   1. Maintains state for a specific client.
   2. Useful for multi-call workflows.

2. **Cons:**

   1. Increased memory per client.
   2. Requires bindings that support sessions.

### 3. Single (Singleton)

- **A single service instance serves all client requests.**
- Shared state across all clients.

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MyService : IMyService
{
    // Shared instance for all clients
}
```

1. **Pros:**

   1. Easy to maintain shared state.
   2. Suitable for centralized services.

2. **Cons:**

   1. Must handle concurrency (`ConcurrencyMode.Multiple` or `Reentrant`).
   2. Potential bottleneck if not managed correctly.

### Summary Table

| Mode | Instance per | State Preservation | Concurrency Consideration |
| --- | --- | --- | --- |
| PerCall | Call | No | Not needed |
| PerSession | Session | Yes (per client) | Needed if multiple calls overlap |
| Single | Application | Yes (shared) | Must handle explicitly |

You can configure it like this:

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService
{
    // Implementation
}
```

## Answers

### Answer by ICSM Computer

In **WCF (Windows Communication Foundation)**, **instance management** defines **how service objects are created and maintained** for client requests. WCF provides three main **instance context modes**, configured using the `[ServiceBehavior]` attribute's `InstanceContextMode` property.

### 1. PerCall (default)

- **A new service instance is created for each client request.**
- No state is maintained between calls.
- Ideal for **stateless services**.

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService
{
    // A new instance is created per request
}
```

1. **Pros:**

   1. High scalability.
   2. No need for synchronization.
   3. Low memory usage.

2. **Cons:**

   1. Cannot maintain service-level state.

### 2. PerSession

- **One service instance is created per client session** (requires session-enabled binding like `NetTcpBinding` or `WSHttpBinding` with reliable session).
- State is preserved throughout the session.

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService
{
    // Maintains state per client session
}
```

1. **Pros:**

   1. Maintains state for a specific client.
   2. Useful for multi-call workflows.

2. **Cons:**

   1. Increased memory per client.
   2. Requires bindings that support sessions.

### 3. Single (Singleton)

- **A single service instance serves all client requests.**
- Shared state across all clients.

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MyService : IMyService
{
    // Shared instance for all clients
}
```

1. **Pros:**

   1. Easy to maintain shared state.
   2. Suitable for centralized services.

2. **Cons:**

   1. Must handle concurrency (`ConcurrencyMode.Multiple` or `Reentrant`).
   2. Potential bottleneck if not managed correctly.

### Summary Table

| Mode | Instance per | State Preservation | Concurrency Consideration |
| --- | --- | --- | --- |
| PerCall | Call | No | Not needed |
| PerSession | Session | Yes (per client) | Needed if multiple calls overlap |
| Single | Application | Yes (shared) | Must handle explicitly |

You can configure it like this:

```cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService
{
    // Implementation
}
```


---

Original Source: https://www.mindstick.com/interview/34169/what-are-the-different-instance-management-modes-available-in-wcf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
