{"id":270,"date":"2013-09-29T10:05:33","date_gmt":"2013-09-29T10:05:33","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=270"},"modified":"2013-09-29T10:05:33","modified_gmt":"2013-09-29T10:05:33","slug":"understanding-instance-management-in-wcf","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/09\/29\/understanding-instance-management-in-wcf\/","title":{"rendered":"Understanding Instance Management in WCF"},"content":{"rendered":"<p>instance management is the technique WCF uses to bind client requests to service instances, governing which service instance handles which client request, and when. WCF Supports 3 types of instance activation<\/p>\n<ul>\n<li><strong>Per Call<\/strong> : per call services allocate and destroy a new service instance for each&#160; client request. <\/li>\n<li><strong>Per Session<\/strong>: session full services allocate a service instance for each client&#160; connection. <\/li>\n<li><strong>Singleton<\/strong>: singleton services share the same service instance for all clients, across all connections and activations. <\/li>\n<\/ul>\n<p>by and large the service instance mode is strictly a server side implementation detail, hat should not manifest itself on the client side in any way.<\/p>\n<p>now let&#8217;s define a service, that demonstrates the instance management in real life.<\/p>\n<h3>Creating the Class Library<\/h3>\n<p>let&#8217;s create a service, create a new project, take a Class Library template, name it <strong>InstanceLib<\/strong>, let&#8217;s do some house keeping with the newly created project.<\/p>\n<ul>\n<li>Delete the file<strong> Class1.cs<\/strong> from the project workspace. <\/li>\n<li>Add a new <strong>Interface<\/strong> named <strong>ISingleCallService<\/strong> to the project, a new file <strong>ISingleCallService.cs<\/strong> will be added to the project. <\/li>\n<li>Add a new <strong>Class <\/strong>named <strong>SingleCallService<\/strong>, to the project. that will implement the <strong>ISingleCallService<\/strong> interface, a new file <strong>SingleCallService.cs<\/strong> will be added to the project. <\/li>\n<li>Add a new <strong>Interface<\/strong> named <strong>ISessionService<\/strong> to the project, a new file <strong>ISessionService.cs<\/strong> will be added to the project. <\/li>\n<li>Add a new <strong>Class<\/strong> named <strong>SessionService<\/strong>, to the project. that will implement the <strong>ISessionService <\/strong>interface, a new file <strong>SessionService.cs<\/strong> will be added to the project. <!--EndFragment--><\/li>\n<li>Add a new Interface named <strong>ISingletonService<\/strong> to the project, a new file <strong>ISingletonService.cs<\/strong> will be added to the project. <\/li>\n<li>Add a new Class named <strong>SingletonService<\/strong>, to the project. that will implement the <strong>ISingletonService<\/strong> interface, a new file <strong>SingletonService.cs<\/strong> will be added to the project. <\/li>\n<\/ul>\n<p>to keep the things simple, let&#8217;s assume that each interface defines only one method, which simply adds a passed value, to&#160; its member variable.<\/p>\n<h4>Defining Interfaces<\/h4>\n<p>so let&#8217;s define interface for each service.<\/p>\n<p><strong>\/\/ Listing of ISingleCallService.cs<\/strong><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>using System;        <br \/>using System.Text;         <br \/>using System.ServiceModel;<\/strong><\/font><\/p>\n<p><strong><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">namespace InstanceLib        <br \/>{         <br \/>&#160;&#160;&#160; [ServiceContract]         <br \/>&#160;&#160;&#160; interface ISingleCallService         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; [OperationContract]         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; double AddValue (double dblNum);         <br \/>&#160;&#160;&#160; }         <br \/>}         <br \/><\/font><\/strong><\/p>\n<p><strong>\/\/ Listing of ISessionService.cs<\/strong><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>using System;        <br \/>using System.Text;         <br \/>using System.ServiceModel;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>namespace InstanceLib        <br \/>{         <br \/>&#160;&#160;&#160; [ServiceContract]         <br \/>&#160;&#160;&#160; interface ISessionService         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; [OperationContract]         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; double AddValue (double dblNum);         <br \/>&#160;&#160;&#160; }         <br \/>}<\/strong><\/font><\/p>\n<p><strong>\/\/ Listing of ISingletonService.cs<\/strong><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>using System;        <br \/>using System.Collections.Generic;         <br \/>using System.Linq;         <br \/>using System.Text;         <br \/>using System.ServiceModel;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>namespace InstanceLib        <br \/>{         <br \/>&#160;&#160;&#160; [ServiceContract]         <br \/>&#160;&#160;&#160; interface ISingletonService         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; [OperationContract]         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; double AddValue (double dblNum);         <br \/>&#160;&#160;&#160; }         <br \/>}<\/strong><\/font>     <\/p>\n<ul><!--EndFragment--><!--EndFragment--><\/ul>\n<h4>Implementing Interfaces<\/h4>\n<p>Let&#8217;s implement each interface,as shown below.<\/p>\n<p><strong>\/\/ Listing of SingleCallService.cs<\/strong><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>using System;        <br \/>using System.Text;         <br \/>using System.ServiceModel ;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>namespace InstanceLib        <br \/>{         <br \/>&#160;&#160;&#160; [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]&#160;&#160; <br \/>&#160;&#160;&#160; public class SingleCallService : ISingleCallService         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; private double m_dblTotal = 0 ;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; public double AddValue(double dblVal)        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_dblTotal += dblVal;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return m_dblTotal;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160; }         <br \/>}<\/strong><\/font><\/p>\n<h5>Important<\/h5>\n<p>You can notice <strong>ServiceBehavior<\/strong> attribute of the class, which has been defined as <strong>InstanceContextMode.PerCall,<\/strong> it specifies that a new InstanceContext object is created prior to and recycled subsequent to each calll. if you omit this attribute or do not specify, then <strong>InstanceContextMode.PerCall<\/strong> is assumed by default.<\/p>\n<p><strong>\/\/ Listing of SessionService.cs<\/strong><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>using System;        <br \/>using System.Text;         <br \/>using System.ServiceModel;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>namespace InstanceLib        <br \/>{         <br \/>&#160;&#160;&#160; [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]         <br \/>&#160;&#160;&#160; public class SessionService : ISessionService         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; private double m_dblTotal = 0 ;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; public double AddValue(double dblVal)        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_dblTotal += dblVal;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return m_dblTotal;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160; }         <br \/>}<\/strong><\/font>     <\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong><\/strong><\/font><\/p>\n<h5>Important<\/h5>\n<p>You can notice <strong>ServiceBehavior<\/strong> attribute of the class, which has been defined as <strong>InstanceContextMode.PerSession, <\/strong>it specifies that InstanceContext object is created for each session.<\/p>\n<p>&#160;<strong>\/\/ Listing of SingletonService.cs<\/strong><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>using System;        <br \/>using System.Collections.Generic;         <br \/>using System.Linq;         <br \/>using System.Text;         <br \/>using System.ServiceModel;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>namespace InstanceLib        <br \/>{         <br \/>&#160;&#160;&#160; [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]         <br \/>&#160;&#160;&#160; public class SingletonService : ISingletonService         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; private double m_dblTotal = 0 ;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; public double AddValue(double dblVal)        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_dblTotal += dblVal;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return m_dblTotal;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160; }         <br \/>}<\/strong><\/font>     <\/p>\n<h5>Important<\/h5>\n<p>You can notice <strong>ServiceBehavior<\/strong> attribute of the class, which has been defined as <strong>InstanceContextMode.Singlel,<\/strong> it specifies that Only one <strong>InstanceContext<\/strong> object is used for all incoming calls and is not recycled to the calls. if the service object does not exist, one is created.&#160; <\/p>\n<p>if you notice the all 3 service implementation, you will notice the difference in <strong>ServiceBehavior<\/strong> class attribute of each class, apart from that, all interfaces has been implemented has the same implementation. they simply add a passed value to a member variable of the class.<\/p>\n<p>Build the project and your Class Library is ready.&#160; <\/p>\n<h3>Hosting the Service<\/h3>\n<p>for the sake of simplicity, i have have hosted this service as a self hosted service, to create the Host application, create a new <strong>Console based application<\/strong>.<\/p>\n<p>Before we write any code for the Host application, let&#8217;s define the configuration file for the Service(s).<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;      <br \/>&lt;configuration&gt;       <br \/>&#160; &lt;system.serviceModel&gt;       <br \/>&#160;&#160;&#160; &lt;services&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160; &lt;!&#8211;************************ Single Call Service ************************ &#8211;&gt;      <br \/>&#160;&#160;&#160;&#160;&#160; <strong>&lt;service name=&quot;InstanceLib.SingleCallService&quot; behaviorConfiguration=&quot;<font color=\"#800000\">SingleCallServiceBehavior<\/font>&quot;&gt;         <br \/><\/strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;host&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;baseAddresses&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add baseAddress=&quot;<\/font><a href=\"http:\/\/localhost:9011\/SingleCallService&quot;\/\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9011\/SingleCallService&quot;\/<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add baseAddress=&quot;<strong>net.tcp:\/\/localhost:9012\/SingleCallService<\/strong>&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/baseAddresses&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/host&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;<\/font><a href=\"http:\/\/localhost:9011\/SingleCallService&quot;\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9011\/SingleCallService&quot;<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"> binding=&quot;wsHttpBinding&quot; contract=&quot;InstanceLib.ISingleCallService&quot;\/&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;<strong>net.tcp:\/\/localhost:9012\/SingleCallService<\/strong>&quot; binding=&quot;netTcpBinding&quot; contract=&quot;InstanceLib.ISingleCallService&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;mex&quot; binding=&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;mex&quot; binding=&quot;mexTcpBinding&quot; contract=&quot;IMetadataExchange&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/service&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160; &lt;!\u2014************************ Single Session Service ************************&#8211;&gt;      <br \/>&#160;&#160;&#160;&#160; <strong> &lt;service name=&quot;InstanceLib.SessionService&quot; behaviorConfiguration=&quot;<font color=\"#ff0000\">SessionServiceBehavior<\/font>&quot;&gt;         <br \/><\/strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;host&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;baseAddresses&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add baseAddress=&quot;<\/font><a href=\"http:\/\/localhost:9013\/SessionService&quot;\/\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9013\/SessionService&quot;\/<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add baseAddress=&quot;<strong>net.tcp:\/\/localhost:9014\/SessionService<\/strong>&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/baseAddresses&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/host&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;<\/font><a href=\"http:\/\/localhost:9013\/SessionService&quot;\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9013\/SessionService&quot;<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"> binding=&quot;wsHttpBinding&quot; contract=&quot;InstanceLib.ISessionService&quot;\/&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;<strong>net.tcp:\/\/localhost:9014\/SessionService<\/strong>&quot; binding=&quot;netTcpBinding&quot; contract=&quot;InstanceLib.ISessionService&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;mex&quot; binding=&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;mex&quot; binding=&quot;mexTcpBinding&quot; contract=&quot;IMetadataExchange&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/service&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160; &lt;!\u2014************************** Singleton Service **************************&#8211;&gt;      <br \/>&#160;&#160;&#160;&#160; <strong> &lt;service name=&quot;InstanceLib.SingletonService&quot; behaviorConfiguration=&quot;<font color=\"#008000\">SingletonServiceBehavior<\/font>&quot;&gt;<\/strong>       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;host&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;baseAddresses&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add baseAddress=&quot;<\/font><a href=\"http:\/\/localhost:9015\/SingletonService&quot;\/\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9015\/SingletonService&quot;\/<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add baseAddress=&quot;net.tcp:\/\/localhost:9016\/SingletonService&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/baseAddresses&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/host&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;<\/font><a href=\"http:\/\/localhost:9015\/SingletonService&quot;\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9015\/SingletonService&quot;<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"> binding=&quot;wsHttpBinding&quot; contract=&quot;InstanceLib.ISingletonService&quot;\/&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;net.tcp:\/\/localhost:9016\/SingletonService&quot; binding=&quot;netTcpBinding&quot; contract=&quot;InstanceLib.ISingletonService&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;mex&quot; binding=&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;endpoint address=&quot;mex&quot; binding=&quot;mexTcpBinding&quot; contract=&quot;IMetadataExchange&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/service&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; &lt;\/services&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; &lt;!&#8211; **************************** behaviors ************************** &#8211;&gt;      <br \/>&#160;&#160;&#160; &lt;behaviors&gt;       <br \/>&#160;&#160;&#160;&#160;&#160; &lt;serviceBehaviors&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;!&#8211; Single Call Service Behavior &#8211;&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;behavior name=&quot;<font color=\"#800000\"><strong>SingleCallServiceBehavior<\/strong><\/font>&quot;&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;serviceMetadata httpGetEnabled=&quot;true&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/behavior&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;!&#8211;Single Session Service Behavior &#8211;&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;behavior name=&quot;<font color=\"#ff0000\"><strong>SessionServiceBehavior<\/strong><\/font>&quot;&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;serviceMetadata httpGetEnabled=&quot;true&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/behavior&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;!&#8211;Singleton Service Behavior &#8211;&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;behavior name=&quot;<font color=\"#008000\"><strong>SingletonServiceBehavior<\/strong><\/font>&quot;&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;serviceMetadata httpGetEnabled=&quot;true&quot;\/&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/behavior&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160; &lt;\/serviceBehaviors&gt;      <br \/>&#160;&#160;&#160; &lt;\/behaviors&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160; &lt;\/system.serviceModel&gt;      <br \/>&lt;\/configuration&gt;<\/font><\/p>\n<p>Now let&#8217;s analyze the Configuration file.<\/p>\n<h4>Single Call service. <\/h4>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&lt;!&#8211;******************* Single Call Service ************************** &#8211;&gt;        <br \/><\/strong>&#160;&#160;&#160;&#160;&#160; &lt;service name=&quot;InstanceLib.SingleCallService&quot; behaviorConfiguration=&quot;SingleCallServiceBehavior&quot;&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;host&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong><font color=\"#000000\"> <\/font><font color=\"#000000\">&lt;baseAddresses&gt;<\/font>         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add <font color=\"#000000\">baseAddress<\/font>=&quot;<\/strong><\/font><a href=\"http:\/\/localhost:9011\/SingleCallService&quot;\/\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9011\/SingleCallService&quot;\/<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&gt;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add <font color=\"#000000\">baseAddress<\/font>=&quot;net.tcp:\/\/localhost:9012\/SingleCallService&quot;\/&gt;         <br \/><\/strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong><font color=\"#000000\"> &lt;\/baseAddresses&gt;          <br \/><\/font><\/strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/host&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<strong><font color=\"#000000\">endpoint<\/font><\/strong> address=&quot;<\/font><a href=\"http:\/\/localhost:9011\/SingleCallService&quot;\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9011\/SingleCallService&quot;<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"> binding=&quot;wsHttpBinding&quot; contract=&quot;InstanceLib.ISingleCallService&quot;\/&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<strong><font color=\"#000000\">endpoint<\/font><\/strong> address=&quot;<strong>net.tcp:\/\/localhost:9012\/SingleCallService<\/strong>&quot; binding=&quot;netTcpBinding&quot; contract=&quot;InstanceLib.ISingleCallService&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<strong><font color=\"#000000\">endpoint<\/font><\/strong> address=&quot;mex&quot; binding=&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<strong><font color=\"#000000\">endpoint<\/font><\/strong> address=&quot;mex&quot; binding=&quot;mexTcpBinding&quot; contract=&quot;IMetadataExchange&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/service&gt;<\/font><\/p>\n<h4>Session service. <\/h4>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&lt;!\u2014************************* Single Session Service ************************* &#8211;&gt;        <br \/><\/strong>&#160;&#160;&#160;&#160;&#160; &lt;service name=&quot;InstanceLib.SessionService&quot; behaviorConfiguration=&quot;SessionServiceBehavior&quot;&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;host&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color=\"#000000\"><strong>&lt;baseAddresses&gt;          <br \/><\/strong><\/font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add <font color=\"#000000\"><strong>baseAddress<\/strong><\/font>=&quot;<\/font><a href=\"http:\/\/localhost:9013\/SessionService&quot;\/\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9013\/SessionService&quot;\/<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add <font color=\"#000000\"><strong>baseAddress<\/strong><\/font>=&quot;<strong>net.tcp:\/\/localhost:9014\/SessionService<\/strong>&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong><font color=\"#000000\">&#160; &lt;\/baseAddresses&gt;<\/font><\/strong>       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/host&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<strong><font color=\"#000000\">endpoint<\/font><\/strong> address=&quot;<\/font><a href=\"http:\/\/localhost:9013\/SessionService&quot;\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9013\/SessionService&quot;<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"> binding=&quot;wsHttpBinding&quot; contract=&quot;InstanceLib.ISessionService&quot;\/&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<strong><font color=\"#000000\">endpoint<\/font><\/strong> address=&quot;net.tcp:\/\/localhost:9014\/SessionService&quot; binding=&quot;netTcpBinding&quot; contract=&quot;InstanceLib.ISessionService&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<strong><font color=\"#000000\">endpoint<\/font><\/strong> address=&quot;<strong>mex<\/strong>&quot; binding=&quot;<strong>mexHttpBinding<\/strong>&quot; contract=&quot;<strong>IMetadataExchange<\/strong>&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<strong><font color=\"#000000\">endpoint<\/font><\/strong> address=&quot;<strong>mex<\/strong>&quot; binding=&quot;<strong>mexTcpBinding<\/strong>&quot; contract=&quot;<strong>IMetadataExchange<\/strong>&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/service&gt;<\/font><\/p>\n<h4>Singleton service. <\/h4>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&lt;!\u2014************************** Singleton Service **************************&#8211;&gt; <\/strong>      <br \/>&#160;&#160;&#160;&#160; <strong> &lt;service name=&quot;InstanceLib.SingletonService&quot; behaviorConfiguration=&quot;<font color=\"#008000\">SingletonServiceBehavior<\/font>&quot;&gt;<\/strong>       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;host&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color=\"#000000\"><strong>&lt;baseAddresses&gt;<\/strong><\/font>       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add <font color=\"#000000\"><strong>baseAddress<\/strong><\/font>=&quot;<\/font><a href=\"http:\/\/localhost:9015\/SingletonService&quot;\/\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9015\/SingletonService&quot;\/<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;add <font color=\"#000000\"><strong>baseAddress<\/strong><\/font>=&quot;<strong>net.tcp:\/\/localhost:9016\/SingletonService<\/strong>&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color=\"#000000\"><strong>&lt;\/baseAddresses&gt;          <br \/><\/strong><\/font>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/host&gt;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<font color=\"#000000\"><strong>endpoint<\/strong><\/font> address=&quot;<\/font><a href=\"http:\/\/localhost:9015\/SingletonService&quot;\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9015\/SingletonService&quot;<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"> binding=&quot;wsHttpBinding&quot; contract=&quot;InstanceLib.ISingletonService&quot;\/&gt;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<font color=\"#000000\"><strong>endpoint<\/strong><\/font> address=&quot;<strong>net.tcp:\/\/localhost:9016\/SingletonService<\/strong>&quot; binding=&quot;netTcpBinding&quot; contract=&quot;InstanceLib.ISingletonService&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<font color=\"#000000\"><strong>endpoint<\/strong><\/font> address=&quot;<strong>mex<\/strong>&quot; binding=&quot;<strong>mexHttpBinding<\/strong>&quot; contract=&quot;<strong>IMetadataExchange<\/strong>&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;<strong><font color=\"#000000\">endpoint<\/font><\/strong> address=&quot;<strong>mex<\/strong>&quot; binding=&quot;<strong>mexTcpBinding<\/strong>&quot; contract=&quot;<strong>IMetadataExchange<\/strong>&quot;\/&gt;       <br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/service&gt;<\/font><\/p>\n<p>&#160;<\/p>\n<h4>Writing code to host the service.<\/h4>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">\/\/ Listing of Program.cs<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">using System;     <br \/>using System.Text;      <br \/>using System.ServiceModel;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">namespace InstanceLibHost     <br \/>{      <br \/>&#160;&#160;&#160; class Program      <br \/>&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; static void Main(string[] args)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ServiceHost m_SingleCallHost = null;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ServiceHost m_SingleSessHost = null;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ServiceHost m_SingletonHost = null;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;\\nHosting Single Call Service at &gt;&gt; &quot; );     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;&#160;&#160;&#160; <\/font><a href=\"http:\/\/localhost:9011\/SingleCallService&quot;);\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9011\/SingleCallService&quot;);<\/font><\/a>    <br \/><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;&#160;&#160;&#160; net.tcp:\/\/localhost:9012\/SingleCallService&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; try      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleCallHost = new ServiceHost(typeof(InstanceLib.SingleCallService));      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleCallHost.Open();      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; catch (Exception eX)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Failed while starting Single Call Service Service [&quot; + eX.Message + &quot;]&quot;);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleCallHost = null;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ( m_SingleCallHost!= null ) Console.WriteLine(&quot;Single Call Service hosted successfully . . .&quot;);<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;\\nHosting Single Session Service at &gt;&gt; &quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;&#160;&#160;&#160; <\/font><a href=\"http:\/\/localhost:9013\/SessionService&quot;);\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9013\/SessionService&quot;);<\/font><\/a>    <br \/><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;&#160;&#160;&#160; net.tcp:\/\/localhost:9014\/SessionService&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; try      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleSessHost = new ServiceHost(typeof(InstanceLib.SessionService));      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleSessHost.Open();      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; catch (Exception eX)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Failed while starting Single Session Service [&quot; + eX.Message + &quot;]&quot;);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleSessHost = null;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (m_SingleSessHost != null) Console.WriteLine(&quot;Single Session Service hosted successfully . . .&quot;);<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;\\nHosting Singlton Service at &gt;&gt; &quot; );     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;&#160;&#160;&#160; <\/font><a href=\"http:\/\/localhost:9015\/SingletonService&quot;\"><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">http:\/\/localhost:9015\/SingletonService&quot;<\/font><\/a><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"> );     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;&#160;&#160;&#160; net.tcp:\/\/localhost:9016\/SingletonService&quot;);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; try      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingletonHost = new ServiceHost(new InstanceLib.SingletonService());      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingletonHost.Open();      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; catch (Exception eX)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Failed while starting Service [&quot; + eX.Message + &quot;]&quot;);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingletonHost = null;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (m_SingletonHost != null) Console.WriteLine(&quot;Singleton Service hosted successfully . . .&quot;);<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Press any key to close . . .&quot; );     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.ReadKey();<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleCallHost.Close ();     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleSessHost.Close ();       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingletonHost.Close () ;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleCallHost = null;     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingleSessHost = null;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_SingletonHost = null;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160; }      <br \/>}<\/font><\/p>\n<p>Build and Execute the Service.<\/p>\n<p>Open a command prompt and execute the host. here is the output.<\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image28.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb34.png\" width=\"669\" height=\"239\" \/><\/a><\/p>\n<p>Now when the service is hosted and running, let&#8217;s now create a client, which is actually going to use this service.<\/p>\n<h3>Creating Client<\/h3>\n<h4>Creating the base project <\/h4>\n<p>Take a console based application. <\/p>\n<h4>Generating proxies <\/h4>\n<p>while the host application is running, right click on the client application project, click on <\/p>\n<h5>Generate Proxy for SingleCallService.<\/h5>\n<p><strong><font size=\"2\">References \u2013&gt; Add Service Reference <\/font><\/strong><\/p>\n<p>in the address bar type the address of the <strong>mex<\/strong> endpoint address of <strong>SingleCallService<\/strong> as shown below.<\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image29.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb35.png\" width=\"548\" height=\"406\" \/><\/a><\/p>\n<p>in the namespace type some good name, let&#8217;s say <strong>SingleCallServiceReference<\/strong> for instance.<\/p>\n<p>hit OK, this will simply add a new file <strong>app.config<\/strong> to the project. and a new item in the Service Reference node of the project.<\/p>\n<h5>Generate Proxy for SessionService.<\/h5>\n<p><strong><font size=\"2\">References \u2013&gt; Add Service Reference <\/font><\/strong><\/p>\n<p>in the address bar type the address of the <strong>mex<\/strong> endpoint address of <strong>SessionService<\/strong> as shown below.<\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image30.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb36.png\" width=\"557\" height=\"376\" \/><\/a><\/p>\n<p>in the namespace type some good name, let&#8217;s say <strong>SessionServiceReference<\/strong> for instance. <\/p>\n<h5>Generate Proxy for SingletonService.<\/h5>\n<p><strong><font size=\"2\">References \u2013&gt; Add Service Reference <\/font><\/strong><\/p>\n<p>in the address bar type the address of the <strong>mex<\/strong> endpoint address of <strong>SingletonService<\/strong> as shown below.<\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image31.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb37.png\" width=\"572\" height=\"412\" \/><\/a><\/p>\n<p>in the namespace type some good name, let&#8217;s say <strong>SingletonServiceReference<\/strong> for instance. <\/p>\n<p>Now when you have added references of all 3 services, let&#8217;s write code for the client to use these services.<\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">\/\/ Listing of Program.cs<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">using System;     <br \/>using System.Text;<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">namespace InstanceClient     <br \/>{      <br \/>class Program      <br \/>&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; static void Main(string[] args)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong>SingleCallServiceReference.SingleCallServiceClient<\/strong> objSvc1 = new <strong>SingleCallServiceReference.SingleCallServiceClient<\/strong>(&quot;WSHttpBinding_ISingleCallService&quot;);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong>SingleCallServiceReference.SingleCallServiceClient<\/strong> objSvc2 = new <strong>SingleCallServiceReference.SingleCallServiceClient<\/strong>(&quot;NetTcpBinding_ISingleCallService&quot;);<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Client 1 Calling Single Call Service 3 times Using Http Binding&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; 3; nI++)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblValue1 = 10;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult1 = objSvc1.AddValue(dblValue1);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Using HTTP Binding &gt;&gt; Input Value : {0:F2} Return value : {1:F2}&quot;, dblValue1, dblResult1);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Client 2 Calling Single Call Service 3 times using TCP Binding&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; 3; nI++)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblValue2 = 10;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult2 = objSvc2.AddValue(dblValue2);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Using TCP&#160; Binding &gt;&gt; Input Value : {0:F2} Return value : {1:F2}&quot;, dblValue2, dblResult2);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SessionServiceReference.SessionServiceClient objSvc3 = new SessionServiceReference.SessionServiceClient(&quot;WSHttpBinding_ISessionService&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SessionServiceReference.SessionServiceClient objSvc4 = new SessionServiceReference.SessionServiceClient(&quot;NetTcpBinding_ISessionService&quot;);<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;\\n\\nClient 1 Calling Single Session Service 3 times Using Http Binding&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; 3; nI++)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblValue1 = 10;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult1 = objSvc3.AddValue(dblValue1);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Using HTTP Binding &gt;&gt; Input Value : {0:F2} Return value : {1:F2}&quot;, dblValue1, dblResult1);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Client 2 Calling Single Session Service 3 times using TCP Binding&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; 3; nI++)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblValue2 = 10;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult2 = objSvc4.AddValue(dblValue2);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Using TCP&#160; Binding &gt;&gt; Input Value : {0:F2} Return value : {1:F2}&quot;, dblValue2, dblResult2);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SingletonServiceReference.SingletonServiceClient objSvc5 = new SingletonServiceReference.SingletonServiceClient(&quot;WSHttpBinding_ISingletonService&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SingletonServiceReference.SingletonServiceClient objSvc6 = new SingletonServiceReference.SingletonServiceClient(&quot;NetTcpBinding_ISingletonService&quot;);<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;\\n\\nClient 1 Calling Singleton Service 3 times Using Http Binding&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; 3; nI++)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblValue1 = 10;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult1 = objSvc5.AddValue(dblValue1);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Using HTTP Binding &gt;&gt; Input Value : {0:F2} Return value : {1:F2}&quot;, dblValue1, dblResult1);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Client 2 Calling Singleton Service 3 times using TCP Binding&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; 3; nI++)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblValue2 = 10;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult2 = objSvc6.AddValue(dblValue2);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Using TCP&#160; Binding &gt;&gt; Input Value : {0:F2} Return value : {1:F2}&quot;, dblValue2, dblResult2);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Press any key to close . . . &quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.ReadKey();      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160; }      <br \/>}      <br \/><\/font><\/p>\n<p><p>&#160;<\/p>\n<p>Open command prompt and execute the client to see the output.<\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image32.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb38.png\" width=\"595\" height=\"280\" \/><\/a><\/p>\n<p>Open one more command prompt, and execute another instance of client.<\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image33.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/09\/image_thumb39.png\" width=\"602\" height=\"260\" \/><\/a><\/p>\n<p>as you can see from the output, all the instance mode in action.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>instance management is the technique WCF uses to bind client requests to service instances, governing which service instance handles which client request, and when. WCF Supports 3 types of instance activation Per Call : per call services allocate and destroy a new service instance for each&#160; client request. Per Session: session full services allocate a&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/09\/29\/understanding-instance-management-in-wcf\/\">Continue reading <span class=\"screen-reader-text\">Understanding Instance Management in WCF<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,12],"tags":[25,33],"class_list":["post-270","post","type-post","status-publish","format-standard","hentry","category-net","category-codeproject","category-soa","tag-instance-management","tag-wcf","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=270"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/270\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}