Sequencing Feature in WCF
Sequencing Feature in WCF
By : Kasim Wirama, MCDBA, MVP SQL Server
Sometimes, in some client scenarios, you need to have initial method and last method. The other methods are between of the two. With some settings, WCF can cover this possibility with turning on session on service contract.
In service contract, you specify IsInitiating to true for Initial method, and IsTerminating to true+IsInitiating to false for last method, as the following code shows:
[OperationContract(Name = "StringMethod",IsInitiating = true)]
string StringMethod();
[OperationContract(Name = "Add",IsInitiating = false)]
int Add(int a, int b);
[OperationContract(Name = "Multiply", IsInitiating = false)]
int Multiply(int a, int b);
[OperationContract(Name = "AddTwo", IsInitiating = false)]
int AddTwo(int a);
[OperationContract(Name = "MultiplyTwo", IsInitiating = false,IsTerminating=true)]
int MultiplyTwo(int a);
For interface implementing class, you set InstanceContextMode to Session.
Because there have been changes in service contract, you can adjust manually at client proxy or regenerate proxy and update to WCF client.
In WCF client, you can call Add method with initial method to StringMethod, otherwise WCF client will get error.
OperationImpl.OperationClient proxy = new OperationClient("myclient");
proxy.Open();
proxy.StringMethod();
int a, b;
a = 5;
b = 6;
Console.WriteLine("original value a : {0} and b : {1}", a, b);
int result;
result= proxy.Add(a, b);
Console.WriteLine("add result : {0}",result);
result = proxy.Add(a, b);
Console.WriteLine("add result : {0}", result);
First Add method result will display 11, second Add will be 22.
Session will end if you call MultipleTwo because, based on service contract, MultipleTwo methods is decorated with IsTerminating to true. If you add code Proxy.Add(a,b) after MultipleTwo method, the session is not valid and it will throw exception.
If you need new session, you need to call StringMethod method as initial method in WCF service at server side.
With simple modification, you can implement sequencing capability in WCF.