InstanceContextMode Enum

Definition

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

public enum InstanceContextMode
Inheritance
InstanceContextMode

Fields

Name Value Description
PerSession 0

A new InstanceContext object is created for each session. If the channel does not create a session this value behaves as if it were PerCall.

PerCall 1

A new InstanceContext object is created prior to and recycled subsequent to each call.

Single 2

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.

Examples

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

// Service class which implements the service contract.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class CalculatorService : ICalculator
{
    public double Add(double n1, double n2)
    {
        return n1 + n2;
    }

    public double Subtract(double n1, double n2)
    {
        return n1 - n2;
    }

    public double Multiply(double n1, double n2)
    {
        return n1 * n2;
    }

    public double Divide(double n1, double n2)
    {
        return n1 / n2;
    }
}

Remarks

The System.ServiceModel.InstanceContext class 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 Instancing.

The Single value specifies that a single InstanceContext object should be used for the lifetime of the service. Several caveats are associated with the Single value:

Applies to

Produkt Verze
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also