InstanceContextMode 枚举

定义

指定可用来处理包含在传入消息中的调用的服务实例数。Specifies the number of service instances available for handling calls that are contained in incoming messages.

public enum class InstanceContextMode
public enum InstanceContextMode
type InstanceContextMode = 
Public Enum InstanceContextMode
继承
InstanceContextMode

字段

PerCall 1

新的 InstanceContext 对象在每次调用前创建,在调用后回收。A new InstanceContext object is created prior to and recycled subsequent to each call.

PerSession 0

为每个会话创建一个新的 InstanceContext 对象。A new InstanceContext object is created for each session. 如果信道未创建会话,则该值的行为就如同 PerCall 一样。If the channel does not create a session this value behaves as if it were PerCall.

Single 2

只有一个 InstanceContext 对象用于所有传入呼叫,并且在调用后不回收。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.

示例

下面的代码演示如何为服务类设置 InstanceContextModeThe 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;
    }
}
' 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

注解

System.ServiceModel.InstanceContext类管理通道和用户定义的服务对象之间的关联。The System.ServiceModel.InstanceContext class manages the association between the channel and the user-defined service objects. 使用 InstanceContextMode 枚举和 ServiceBehaviorAttribute.InstanceContextMode 属性来指定 InstanceContext 对象的生存期。Use the InstanceContextMode enumeration with the ServiceBehaviorAttribute.InstanceContextMode property to specify the lifetime of the InstanceContext object. Windows Communication Foundation (WCF) 可以为每个InstanceContext调用、每个会话创建一个新的对象, 也InstanceContext可以指定将对象绑定到单个服务对象。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.

Single 值指定单个 InstanceContext 对象应该用于服务的整个生存期。The Single value specifies that a single InstanceContext object should be used for the lifetime of the service. Single值相关联的几个警告:Several caveats are associated with the Single value:

适用于

另请参阅