---
title: "What are behaviors in WCF and how do you use them?"  
description: "What are behaviors in WCF and how do you use them?"  
author: "ICSM Computer"  
published: 2025-05-28  
updated: 2025-05-28  
canonical: https://www.mindstick.com/interview/34178/what-are-behaviors-in-wcf-and-how-do-you-use-them  
category: "c#"  
tags: ["c#", "wcf"]  
reading_time: 5 minutes  

---

# What are behaviors in WCF and how do you use them?

**Behaviors** in WCF are extensibility points that allow you to customize or extend the way WCF services, endpoints, operations, or clients behave at runtime.

> They don't change the *message data*, but they can influence *execution*, *security*, *validation*, *instance management*, *error handling*, *logging*, etc.

## Types of WCF Behaviors

| Behavior Type | Applied To | Interface | Purpose |
| --- | --- | --- | --- |
| **Service behavior** | Service-wide | `IServiceBehavior` | Control service instance, concurrency, error handling, etc. |
| **Endpoint behavior** | A single endpoint | `IEndpointBehavior` | Modify client or service endpoints |
| **Operation behavior** | A method | `IOperationBehavior` | Control parameter handling, serialization |
| **Contract behavior** | Interface level | `IContractBehavior` | Modify all operations in a contract |
| **Client behavior** | Client proxy | `IEndpointBehavior` | Customize how a client behaves |

## Example: Service Behavior for Logging

### 1. Implement a custom behavior

```cs
public class LoggingBehavior : IServiceBehavior
{
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher ed in cd.Endpoints)
            {
                ed.DispatchRuntime.MessageInspectors.Add(new LoggingMessageInspector());
            }
        }
    }

    public void AddBindingParameters(...) { }

    public void Validate(...) { }
}
```

### 2. Implement the message inspector

```cs
public class LoggingMessageInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        Console.WriteLine("Request received: " + request.ToString());
        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        Console.WriteLine("Reply sent: " + reply.ToString());
    }
}
```

### 3. Add Behavior to Service Host (programmatically)

```cs
ServiceHost host = new ServiceHost(typeof(MyService));
host.Description.Behaviors.Add(new LoggingBehavior());
host.Open();
```

### 4. Or Add Behavior via Config

You must also create a `BehaviorExtensionElement` and register it in `web.config` to load it via configuration — this is more advanced but allows reuse.

## Built-in Behaviors Examples

| Built-in Behavior | Purpose |
| --- | --- |
| `ServiceMetadataBehavior` | Enables metadata exchange (WSDL) |
| `ServiceDebugBehavior` | Includes exception details in faults |
| `ServiceThrottlingBehavior` | Controls concurrency, sessions, and calls |
| `ServiceAuthorizationBehavior` | Controls access control |
| `ClientViaBehavior` | Controls how clients indicate their URI |

## Summary

1. Behaviors are plug-in points that control how WCF runs.
2. You implement the correct interface (`IServiceBehavior`, `IEndpointBehavior`, etc.)
3. You can apply them programmatically or via configuration.
4. Useful for cross-cutting concerns: logging, error handling, validation, etc.

## Answers

### Answer by ICSM Computer

**Behaviors** in WCF are extensibility points that allow you to customize or extend the way WCF services, endpoints, operations, or clients behave at runtime.

> They don't change the *message data*, but they can influence *execution*, *security*, *validation*, *instance management*, *error handling*, *logging*, etc.

## Types of WCF Behaviors

| Behavior Type | Applied To | Interface | Purpose |
| --- | --- | --- | --- |
| **Service behavior** | Service-wide | `IServiceBehavior` | Control service instance, concurrency, error handling, etc. |
| **Endpoint behavior** | A single endpoint | `IEndpointBehavior` | Modify client or service endpoints |
| **Operation behavior** | A method | `IOperationBehavior` | Control parameter handling, serialization |
| **Contract behavior** | Interface level | `IContractBehavior` | Modify all operations in a contract |
| **Client behavior** | Client proxy | `IEndpointBehavior` | Customize how a client behaves |

## Example: Service Behavior for Logging

### 1. Implement a custom behavior

```cs
public class LoggingBehavior : IServiceBehavior
{
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher ed in cd.Endpoints)
            {
                ed.DispatchRuntime.MessageInspectors.Add(new LoggingMessageInspector());
            }
        }
    }

    public void AddBindingParameters(...) { }

    public void Validate(...) { }
}
```

### 2. Implement the message inspector

```cs
public class LoggingMessageInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        Console.WriteLine("Request received: " + request.ToString());
        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        Console.WriteLine("Reply sent: " + reply.ToString());
    }
}
```

### 3. Add Behavior to Service Host (programmatically)

```cs
ServiceHost host = new ServiceHost(typeof(MyService));
host.Description.Behaviors.Add(new LoggingBehavior());
host.Open();
```

### 4. Or Add Behavior via Config

You must also create a `BehaviorExtensionElement` and register it in `web.config` to load it via configuration — this is more advanced but allows reuse.

## Built-in Behaviors Examples

| Built-in Behavior | Purpose |
| --- | --- |
| `ServiceMetadataBehavior` | Enables metadata exchange (WSDL) |
| `ServiceDebugBehavior` | Includes exception details in faults |
| `ServiceThrottlingBehavior` | Controls concurrency, sessions, and calls |
| `ServiceAuthorizationBehavior` | Controls access control |
| `ClientViaBehavior` | Controls how clients indicate their URI |

## Summary

1. Behaviors are plug-in points that control how WCF runs.
2. You implement the correct interface (`IServiceBehavior`, `IEndpointBehavior`, etc.)
3. You can apply them programmatically or via configuration.
4. Useful for cross-cutting concerns: logging, error handling, validation, etc.


---

Original Source: https://www.mindstick.com/interview/34178/what-are-behaviors-in-wcf-and-how-do-you-use-them

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
