Instance
Management in WCF
Instance management in WCF is the concept in which
we can understand the instance creation of service when client request for the
service. We have to set the InstanceContextMode in ServiceBehavior. There are three type of instance management
available in WCF and these are as follows:-
Per
Call
In per call instance management every time we hit
the service means when we call method it will simply create the new instance of
service and execute the method and give the result to the client. After that it
will simply dispose the instance.
There are no overhead on the server in this type
instance management as resources deallocate as the user request complete. At
every method call new instance will be created in per call.
For
example:-
Interface
Code is:-
[ServiceContract]
public interface IService
{
[OperationContract]
int GetData();
}
Service
Class code is:-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class Service : IService
{
int x = 0;
public int GetData()
{
x++;
return x;
}
}
Web
config code:-
Add this code inside the System.servicemodel tag
<services>
<service name="Service">
<endpoint address="" binding="basicHttpBinding" contract="IService"/>
</service>
</services>
As you can see the binding that I implemented the
basichttpbinding and now create the client for this service:-
ServiceReference1.ServiceClient svc = new ServiceReference1.ServiceClient();
Response.Write("Method Calling First
Time and Result is=" + svc.GetData() + "<br/>");
Response.Write("Method Calling Second
Time and Result is=" + svc.GetData() + "<br/>");
Response.Write("Method Calling Third
Time and Result is=" + svc.GetData() + "<br/>");
Response.Write("Method Calling Fourth
Time and Result is=" + svc.GetData() + "<br/>");
Now you can see the output of this code
in the following image:-
Figure 1
As you can see for every calling it will give the
same result as every time when I call the method it will create the new
instance of the service and accordingly giving the result
Per
Session
Now move to the per session mode. In per session
mode service holds the object for single instance of proxy which created at
client level. In the following example I have created the two proxy object at
the client level. For every instance of proxy service will holds the instance
at service level and will give result accordingly. This is the by default instance mode set in the wcf service. If we do not set any instance mode behavior then by default it will take per Session(Please use wsHttpBinding to check this point)
For example:-
Service implementation of per session:-
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class Service : IService
{
int x = 0;
public int GetData()
{
x++;
return x;
}
}
Web configuration setting for Per Session
<services>
<service name="Service">
<endpoint address="" binding="wsHttpBinding" contract="IService"/>
</service>
</services>
To implement the per session instance management we have to change the
binding as basichttpbinding do not support the per session instance mode.
Now update the service reference at client side and again execute the
code and you will get following output:-
At client side create two instances of proxy and call methods
//proxy instance creation
ServiceReference1.ServiceClient svc = new ServiceReference1.ServiceClient();
Response.Write("Method Calling First
Time with First instance and Result is=" + svc.GetData() + "<br/>");
Response.Write("Method Calling Second
Time with First instance and Result is=" + svc.GetData() + "<br/>");
Response.Write("Method Calling Third
Time with First instance and Result is=" + svc.GetData() + "<br/>");
Response.Write("Method Calling Fourth
Time with First instance and Result is=" + svc.GetData() + "<br/>");
ServiceReference1.ServiceClient svc1 = new ServiceReference1.ServiceClient();
Response.Write("<br/><br/>Method
Calling First Time with Second instance and Result is=" + svc1.GetData() + "<br/>");
Response.Write("Method Calling Second
Time with Second instance and Result is=" + svc1.GetData() + "<br/>");
Response.Write("Method Calling Third
Time with Second instance and Result is=" + svc1.GetData() + "<br/>");
Response.Write("Method Calling Fourth
Time with Second instance and Result is=" + svc1.GetData() + "<br/>");
Output:-
Figure 2
As we can see that service maintain session for each
instance and work accordingly.
Single
In single type of instance management service will create
single object of service for all clients.
For example:-
Set instance mode single
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class Service : IService
{
int x = 0;
public int GetData()
{
x++;
return x;
}
}
Update the service reference and execute the client
application and see the difference:-
Figure 3
As we can see that for single client service created
a single object and we are getting this output.
Now I have created one more .net application means
one more client and added the service reference and call the method and I got
the following output:-
Figure 4
Hope you enjoyed the article
Thanks for sharing valuable information.
ReplyDeletedigital marketing Institute in Delhi
Digital Marketing Course in Noida
SEO course institute in noida
English Speaking Course in Noida
Best English Speaking Course fees Delhi
Hello,
ReplyDeleteGreat Post. It's very Useful Information. In Future, Hope To See More Post. Thanks You For Sharing.
Math Online Tuition In Noida
Electronic Engineering online Tuition
BTech Back Paper Online Tuition
12th Mathematics Tuition In Noida
12th Physics Tuition In Noida
10th Mathematics Tuition In Noida
B.Tech Subjects Tuition In Noida For AKTU University
B.Tech AKTU University Coaching Tuition In Noida
Academy Of Engineers Noida BTech Coaching Institute In Delhi
Hello,
ReplyDeleteGreat Post. It's very Useful Information. In Future, Hope To See More Post. Thanks You For Sharing.
Math Tuition In Noida
B.Tech Math Tuition In Noida
BTech Back Paper Tuition In Noida
BTech Math Tuition In Delhi
B.Com Tuition In Noida
BCA Math Tuition In Noida
Math Tuition Near Me In Noida
Account Tuition In Noida
Online BTech Tutoring Services
Your article was perfect had helped me alot in finding the right thing i had searching this stuff thank you so much for provding me this information.me been very sad after reading this announcement…
ReplyDeletejantar mantar in delhi
insectsight
womens cardigans
tour to kodaikanal
places to visit in naintal
places to visit in delhi
Hello,
ReplyDeleteGreat Post. It's very Useful Information. In Future, Hope To See More Post. Thanks You For Sharing.
CTET Coaching In Noida
UPTET Coaching In Noida
B.Ed Entrance Coaching In Noida
Thanks
Shweta Singh