Redaguoti

Bendrinti naudojant


Default Service Behavior

The Default sample demonstrates how service behavior settings can be configured. The sample is based on the Getting Started, 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 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
AutomaticSessionShutdown Automatically shuts down a session at the client's request.
ConcurrencyMode Specifies the concurrency mode for each service instance.
InstanceContextMode Specifies the instance context mode.
UseSynchronizationContext 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.
IncludeExceptionDetailInFaults Determines whether general unhandled execution exceptions are to be converted into a Fault<string> and sent as a fault message.
TransactionIsolationLevel Specifies the isolation level for transactions.
ValidateMustUnderstand 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
TransactionAutoComplete Determines whether service operation completion commits the current transaction.
TransactionScopeRequired Determines whether the service operation enlists in a client-flowed transaction.
Impersonation Determines whether the service operation impersonates the caller's identity.
ReleaseInstanceMode 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.