OperationContractAttribute.IsInitiating 屬性

定義

取得或設定值,這個值表示某個方法是否實作了可以在伺服器上初始化工作階段的作業 (若這樣的工作階段存在的話)。Gets or sets a value that indicates whether the method implements an operation that can initiate a session on the server (if such a session exists).

public:
 property bool IsInitiating { bool get(); void set(bool value); };
public bool IsInitiating { get; set; }
member this.IsInitiating : bool with get, set
Public Property IsInitiating As Boolean

屬性值

Boolean

如果允許作業在伺服器上初始化工作階段,則為 true,否則為 falsetrue if the operation is permitted to initiate a session on the server, otherwise, false. 預設為 trueThe default is true.

範例

下列範例是一個實作服務合約的服務,此合約指定三個方法。The following example is a service that implements a service contract that specifies three methods. 此服務需要工作階段。The service requires a session. 若呼叫者先呼叫其他作業,而非 MethodOne,通道會拒絕並擲回例外狀況。If a caller's first call is to any operation other than MethodOne, the channel is refused and an exception is thrown. 當呼叫者呼叫 MethodOne 以初始化工作階段時,該呼叫者隨時可呼叫 MethodThree 終止通訊工作階段。When a caller initiates a session by calling MethodOne, that caller can terminate the communication session at any time by calling MethodThree. MethodTwo 可以在會話期間呼叫任意次數。MethodTwo can be called any number of times during a session.

[ServiceContract(SessionMode=SessionMode.Required)]  
public class InitializeAndTerminateService  
{  
  [OperationContract(  
    IsOneWay=true,  
    IsInitiating=true,  
    IsTerminating=false  
  )]  
  public void MethodOne()  
  {  
    return;  
  }  

  [OperationContract(  
    IsInitiating=false,  
    IsTerminating=false  
  )]  
  public int MethodTwo(int x, out int y)  
  {  
    y = 34;  
    return 0;  
  }  

  [OperationContract(  
    IsOneWay=true,  
    IsInitiating=false,  
    IsTerminating=true  
  )]  
  public void MethodThree()  
  {  
    return;  
  }  
}  

備註

IsInitiating 屬性控制作業是否可在工作階段建立時,成為第一個被呼叫的作業。The IsInitiating property controls whether an operation can be the first operation called when a session is created.

注意

ServiceContractAttribute.SessionMode 的值必須為 AllowedRequired,而且使用的繫結必須要求或允許工作階段,IsInitiating 屬性才能正常運作。The value of ServiceContractAttribute.SessionMode must be either Allowed or Required and the binding used must require or allow sessions for the IsInitiating property to work properly.

預設為 true,表示作業可成為通道上第一個被呼叫的作業。The default is true, which means that an operation can be the first one called on a channel. 除了呼叫此方法,後續對初始化方法的呼叫就不再有效用。Subsequent calls to the initiating method have no effect, other than to call the method. 不會再建立其他工作階段。No other sessions are created. 若合約不使用工作階段,則將 IsInitiating 設為 false 的設定會忽略。If the contract does not make use of a session, setting IsInitiating to false is ignored.

一般而言,您可將 IsInitiating 設為 false,強制用戶端在叫用此方法前,先呼叫服務上的另一個方法。Typically, you set IsInitiating to false to force clients to call another method on the service before they can invoke this one. 例如,若您的服務有一系列的作業,這些作業依存於一個訂單 ID 號碼,您可將 服務作業的 設為 ,將其餘服務作業設為 。For example, if your service has a series of operations that depend on an order ID number, you can set IsInitiating to true for a GetOrderId service operation and set all remaining service operations to false. 這樣可確保每個新用戶端在使用服務所公開的其他方法之前,都先取得了訂單 ID。This ensures that each new client obtains an order ID prior to using the other methods exposed by the service.

注意

IsInitiatingAction 屬性之間有互動。There is an interaction between IsInitiating and the Action property. 服務合約中,只能有一個把 Action 屬性設為 "*" 的服務作業。A service contract can have only one service operation with the Action property set to "*". Action * 當 IsInitiating 屬性設定為時,裝載于服務類別所執行之相同接聽 URI 上的任何服務合約群組都可以有許多服務作業,並將屬性設定為 "" falseAny group of service contracts hosted at the same listen URI that a service class implements can have many service operations with the Action property set to "*" when the IsInitiating property is set to false. 不過,只有其中一個服務方法可以將 Action 屬性設定為 " * ",而且 IsInitiating 屬性設定為 trueHowever, only one of those service methods can have the Action property set to "*" and the IsInitiating property set to true.

若服務收到一個非初始化作業的訊息,該服務會傳回 ActionNotSupported SOAP 錯誤。If a service receives a message for a non-initiating operation, the service returns an ActionNotSupported SOAP fault. 用戶端會把它當做 FaultExceptionThe client experiences this as an FaultException. 若用戶端先呼叫非初始化作業,用戶端會擲回 System.InvalidOperationExceptionIf a client calls a non-initiating operation first, the client runtime throws an System.InvalidOperationException.

如需詳細資訊,請參閱 使用會話For more information, see Using Sessions.

適用於