Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
MSDN Library

Default Service Behavior

This sample demonstrates how service behavior settings can be configured. The sample is based on the Getting Started Sample, which implements the ICalculator service contract. This sample explicitly defines service behaviors and operation behaviors using the ServiceBehaviorAttribute and OperationBehaviorAttribute attributes. You can configure behaviors in configuration files or imperatively in code (as this sample demonstrates).

In this sample, the client is a console application (.exe) and the service is hosted by Internet Information Services (IIS).

Note Note

The setup procedure and build instructions for this sample are located at the end of this topic.

The service class specifies behaviors with the ServiceBehaviorAttribute and the OperationBehaviorAttribute as shown in the following code sample. All values specified are the defaults.

public class CalculatorService : ICalculator
    public double Add(double n1, double n2)
        return n1 + n2;

Service behaviors are specified with the ServiceBehaviorAttribute attribute. The following table describes some of these behaviors.

Service behavior



Automatically shuts down a session at the client's request.


Specifies the concurrency mode for each service instance.


Specifies the instance context mode.


Determines whether to use the provided synchronization context, if one is set. Use this when you want to control whether to use a WindowsFormsSynchronizationContext in Windows Forms applications.


Determines whether general unhandled execution exceptions are to be converted into a Fault<string> and sent as a fault message.


Specifies the isolation level for transactions.


Determines whether unexpected message headers cause an error condition.

Operation behaviors are specified by using the OperationBehaviorAttribute attribute. The following table describes some of these behaviors.

Operation Behavior



Determines whether service operation completion commits the current transaction.


Determines whether the service operation enlists in a client-flowed transaction.


Determines whether the service operation impersonates the caller's identity.


Determines whether service instances are recycled at the start or end of the service operation call.

When you run the sample, the operation requests and responses are displayed in the client console window. The delay between the calls is the result of the calls to System.Threading.Thread.Sleep() made in the service operations. The rest of the behavior samples explain these behaviors in more detail. Press ENTER in the client window to shut down the client.

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

Important note Important

The samples may already be installed on your machine. Check for the following (default) directory before continuing.


If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.


© 2015 Microsoft