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. 如果該通道沒有建立工作階段,則此值的作用就如同把此值設為 PerCallIf 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:

適用於

另請參閱