Editar

Partilhar via


OperationContractAttribute.IsInitiating Property

Definition

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

Property Value

true if the operation is permitted to initiate a session on the server, otherwise, false. The default is true.

Examples

The following example is a service that implements a service contract that specifies three methods. The service requires a session. If a caller's first call is to any operation other than MethodOne, the channel is refused and an exception is thrown. When a caller initiates a session by calling MethodOne, that caller can terminate the communication session at any time by calling MethodThree. 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;  
  }  
}  

Remarks

The IsInitiating property controls whether an operation can be the first operation called when a session is created.

Note

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.

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. If the contract does not make use of a session, setting IsInitiating to false is ignored.

Typically, you set IsInitiating to false to force clients to call another method on the service before they can invoke this one. 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. This ensures that each new client obtains an order ID prior to using the other methods exposed by the service.

Note

There is an interaction between IsInitiating and the Action property. A service contract can have only one service operation with the Action property set to "*". Any 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. However, only one of those service methods can have the Action property set to "*" and the IsInitiating property set to true.

If a service receives a message for a non-initiating operation, the service returns an ActionNotSupported SOAP fault. The client experiences this as an FaultException. If a client calls a non-initiating operation first, the client runtime throws an System.InvalidOperationException.

For more information, see Using Sessions.

Applies to