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
The WCF samples may already be installed on your machine. Check this (default) directory before continuing: <InstallDrive>:\Samples\WCFWFCardspaceIf this directory doesn’t exist, click the download sample link at the top of this page. Note that this will download and install all of the WF, WCF, and CardSpace samples, you will only have to do this once. The sample is located in the following directory <InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Service\Behaviors\Default.
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.
[ServiceBehavior(
AutomaticSessionShutdown=true,
ConcurrencyMode=ConcurrencyMode.Single,
InstanceContextMode=InstanceContextMode.PerSession,
IncludeExceptionDetailInFaults=false,
UseSynchronizationContext=true,
ValidateMustUnderstand=true)]
public class CalculatorService : ICalculator
{
[OperationBehavior(
TransactionAutoComplete=true,
TransactionScopeRequired=false,
Impersonation=ImpersonationOption.NotAllowed)]
public double Add(double n1, double n2)
{
System.Threading.Thread.Sleep(1600);
return n1 + n2;
}
...
}
Service behaviors are specified with the ServiceBehaviorAttribute attribute. The following table describes some of these behaviors.
Service behavior | Description |
---|---|
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 | Description |
---|---|
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
Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.
To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.
To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.
© 2007 Microsoft Corporation. All rights reserved.