Demarcating Operations
Sometimes, a sessionful contract has an implied order of operation invocations. Some operations cannot be called first, while other operations must be called last. For example, consider this contract, used to manage customer orders:
[ServiceContract(SessionMode = SessionMode.Required)]
interface IOrderManager
{
[OperationContract]
void SetCustomerId(int customerId);
[OperationContract]
void AddItem(int itemId);
[OperationContract]
decimal GetTotal();
[OperationContract]
bool ProcessOrders();
}The contract has the following constraints: the client must provide the customer ID as the first operation in the session, or else no other operations can take place; items may be added, and the total calculated, as often as the client wishes; processing the order terminates the session, and therefore must come last. In classic .NET, such requirements often forced the developers to support some state machine or state flags and to verify the state on every operation.
WCF, however, allows contract designers to designate contract
operations as operations that can or cannot start or terminate the
session, using the IsInitiating and
IsTerminating
properties of the OperationContract
attribute:
[AttributeUsage(AttributeTargets.Method)]
public sealed class OperationContractAttribute : Attribute
{
public bool IsInitiating
{get;set;}
public bool IsTerminating
{get;set;}
//More members
}These properties can be used to demarcate the boundary of the session; hence, I call this technique ...