inforakesha

A fine WordPress.com site

OperationContract IsInitiating, IsOneWay, IsTerminating

Leave a comment

The IsInitiating parameter specifies whether or not an operation implemented by the associated method can initiate a session on the server. Session instancing is the ability to have separate instances of a class be maintained for each client channel. This property controls whether an operation is allowed to be the first operation called when a session is created. The default for this parameter is true, meaning that the specified operation can be the first called on a channel. In this scenario, all following calls to this method have no effect (meaning, no other sessions are created). If this parameter is set tofalse, the client is forced to call other methods prior to calling this method.

This comes in handy when you are trying to set an “order of operation,” meaning that you need a specific method to be called first because the other methods called depend on something returned from the first method.

For example, the following contains three methods, or service operations. The first operation creates the session and must be the first method called. The final operation, Logout, closes the session:

[ServiceContract]
public interface IBuyStock
{
[OperationContract(IsInitiating = true, IsTerminating = false)]
void Login(user);
[OperationContract(IsInitiating = false, IsTerminating = false)]
void BuyStock(string stocksymbol, int quantity);
[OperationContract(IsInitiating = false, IsTerminating = true)]
void Logout(user);
}

Once the initiating method has been called, subsequent calls can be made to that method with no effect to its initiating properties.

If any method other than the initiating method is called first, the following error is returned:

The operation ‘operationname’ cannot be the first operation to be called because
IsInitiating is false.

The initiating method must be called first, then other operations can be called.

IsOneWay

Service communication by default is bi-directional. Bi-directional service communication means that a service operation can receive incoming messages and send a reply.

The IsOneWay parameter specifies whether a service operation returns a reply message. The default value for this parameter is false, meaning that the method does not return a reply message.

The following example illustrates a one-way communication:

[ServiceContract]
public interface IBuyStock
{
[OperationContract(IsOneWay = true)]
void Login(user);
[OperationContract(IsOneWay = false)]
void BuyStock(string stocksymbol, int quantity);
}

In a one-way communication, the client initiates the communication and continues code execution and does not wait for a response from the service. In a two-way communication, it waits for a response from the service before continuing code execution.

The downside to using one-way communication is that the caller has no way of knowing whether or not the service processed the message successfully.

Any methods that return a value where the IsOneWay property is set to false will return an exception.

IsTerminating

The IsTerminating property specifies whether a called service operation is to terminate the communication session. The following example shows the last call, Logout(), has theIsTerminating property set to true:

[ServiceContract]
public interface IBuyStock
{
[OperationContract(IsInitiating = true, IsTerminating = false)]
void Login(user);
[OperationContract(IsInitiating = false, IsTerminating = false)]
void BuyStock(string stocksymbol, int quantity);
[OperationContract(IsInitiating = false, IsTerminating = true)]
void Logout(user);
}

When the IsTerminating property is set to true, the session is closed after the reply message is sent (if a reply message needs to be sent). On the client side, an IsTerminating value of true tells WCF to close the channel only after the reply arrives at the client.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s