Reputation: 34188
i am very new in WCF. so often gaze for wcf code & article. after viewing many code i often stuck for very basic things and got no elaborate discussion for the below question. so here are my few basic question....and looking for details discussion with sample situation and with sample code.
[OperationContract(IsInitiating=false, IsOneWay=false)]
bool Add_Contact(int sessionkey, string Newusrname);
What is the meaning of IsInitiating=false or true. when i should set it true or false ? what is the meaning of IsOneWay=false or true. when i should set it true or false ?
i belief my question are very basic and lots of scholar every time monitoring this forum. i hope i will get the best answer in details with many situation like when one should choose which option over other option with few sample code for simulating situations. thanks
Upvotes: 3
Views: 4391
Reputation: 1
[OperationContract(IsInitiating = true, IsTerminating = false)]
Blockquote
In above code IsInitiating property is set to true when we want to create session .
Blockquote
We know login method is called first before other methods,hence we use IsInitiating = true for login method and for other methods we set it as false.
Blockquote
In the same way we use Isterminating=True for logout method then session is deleted.
Upvotes: -2
Reputation: 1188
IsInitiating
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 to false
, 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 the IsTerminating
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.
Upvotes: 7