InstanceContextMode Enumeration

 

Specifies the number of service instances available for handling calls that are contained in incoming messages.

Namespace:   System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

Public Enumeration InstanceContextMode

Member nameDescription
PerCall

A new InstanceContext object is created prior to and recycled subsequent to each call. If the channel does not create a session this value behaves as if it were PerCall.

PerSession

A new InstanceContext object is created for each session.

Single

Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created.

The System.ServiceModel.InstanceContext manages the association between the channel and the user-defined service objects. Use the InstanceContextMode enumeration with the ServiceBehaviorAttribute.InstanceContextMode property to specify the lifetime of the InstanceContext object. Windows Communication Foundation (WCF) can create a new InstanceContext object for every call, every session or specify that the InstanceContext object is bound to a single service object. For a working example, see the Instancing.

The Single value specifies that a single InstanceContext object should be used for the lifetime of the service.

System_CAPS_noteNote

If the InstanceContextMode value is set to Single the result is that your service can only process one message at a time unless you also set the ConcurrencyMode value to Multiple.

The following code illustrates how to set the InstanceContextMode for a service class:

' Service class which implements the service contract.
Public Class CalculatorService
Implements ICalculator

    Public Function Add(n1 As Double, n2 As Double) As Double Implements ICalculator.Add
        Return n1 + n2
    End Function

    Public Function Subtract(n1 As Double, n2 As Double) As Double Implements ICalculator.Subtract
        Return n1 - n2
    End Function

    Public Function Multiply(n1 As Double, n2 As Double) As Double Implements ICalculator.Multiply
        Return n1 * n2
    End Function

    Public Function Divide(n1 As Double, n2 As Double) As Double Implements ICalculator.Divide
        Return n1 / n2
    End Function

End Class

.NET Framework
Available since 3.0
Return to top
Show: