Release Instance using WCF
Release Instance using WCF
Below given example make clear the release instance setting, which controls how service class instances will release once its operation completes with respect to the client requests.
Types of operation modes:
None: Recycles the object according to the value.
BeforeCall: Recycles the instance before to calling the operation.
AfterCall: Recycles the instance successive to the completion of the operation.
BeforeAndAfterCall: Recycles the instance before to calling the operation and successive to the completion of the operation.
The service class specifies operation behavior as
[OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.<Operation_Type>)]
//None: Recycles the object according to the value.
[OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.None)]
//BeforeCall: Recycles the instance before to calling the operation.
[OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.BeforeCall)]
//AfterCall: Recycles the instance successive to the completion of the operation.
[OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.AfterCall)]
//BeforeAndAfterCall: Recycles the instance before to calling the operation and successive to the completion of the operation.
[OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.BeforeAndAfterCall)]
The service class contains the following
1. Constructor, which is useful to create new GUID for every instance of service class
2. getInfo(), returns message to the client
3. getInfo2(), returns message to the client
Implementation Steps follows as below:
1) Create new WCF Service Library Project with the name WCFLib
2) Rename Iservice.cs to IServicedClass.cs from Solution Explorer
3) Rename Service.cs to ServicedClass.cs from Solution Explorer
4) Add Reference to System.ServiceModel from Solution Explorer
5) Write the following code in IServicedClass.cs
using System.ServiceModel;
namespace WCFLib
{
[ServiceContract]
public interface IServicedClass
{
[OperationContract]
string getInfo();
[OperationContract]
string getInfo2();
}
}
6) Write the following code in ServicedClass.cs
using System.ServiceModel;
using System.Threading;
namespace WCFLib
{
/* Instance Activation Begin */
//AfterCall will release the object once the call is done on particular method.
// [OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.AfterCall)]
// public string getInfo() { … }
// public string getInfo2() { … }
/* Instance Activation End */
public class ServicedClass:IServicedClass
{
private Guid id;
public ServicedClass()
{
id = Guid.NewGuid();
}
//For every call, object will release only after competion of execution of this method
[OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.AfterCall)]
public string getInfo()
{
Thread.Sleep(2000);
return “*** getInfo called on Object id : “ + id.ToString() + “***”;
}
[OperationBehavior]
public string getInfo2()
{
return “*** getInfo2 called on Object id : “ + id.ToString() + “***”;
}
}
}
Note : Rebuild the service (WCFLib project) after changing the instancing mode.
7) Build only WCFLib project
8) Add new Console Application with the name WCFSerCon in the solution
9) Add Reference to System.ServiceModel
10) Add Reference to WCFLib.dll
11) Write the following code in Program.cs
using System.ServiceModel;
using WCFLib;
namespace WCFSerCon
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(ServicedClass));
string address = “net.tcp://localhost:1099/Myobject”;
NetTcpBinding binding = new NetTcpBinding();
host.AddServiceEndpoint(typeof(IServicedClass), binding, address);
host.Open();
Console.WriteLine(“Server is Ready and Running…”);
Console.ReadLine();
}
}
}
12) Build only WCFSerCon project
13) Add new Console Application with the name WCFClientCon in the solution
14) Add Reference to System.ServiceModel
15) Add Reference to WCFLib.dll
16) Write the following code in Program.cs
using System.Threading;
using System.ServiceModel;
using WCFLib;
namespace WCFClientCon
{
class Program
{
static void Main(string[] args)
{
string address = “net.tcp://localhost:1099/Myobject”;
NetTcpBinding binding = new NetTcpBinding();
ChannelFactory<IServicedClass> cf = new ChannelFactory<IServicedClass>(binding, address);
IServicedClass obj = cf.CreateChannel();
//Instance Activation example
for (int i = 0; i < 5; i++)
{
Console.Write(DateTime.Now.ToLongTimeString());
Console.WriteLine(obj.getInfo());
Console.WriteLine(obj.getInfo2());
Console.WriteLine();
Thread.Sleep(1000);
}
Console.WriteLine();
Console.WriteLine(“Press any key…”);
Console.ReadLine();
}
}
}
17) Build only WCFClientCon project
18) Set WCFSerCon as start up project and Run using ctrl+F5
19) Now server is ready and you can find the following screen
20) Set WCFClientCon as start up project and Run using ctrl+F5
21) Now client application is executing and you can find the following output
We are calling first getInfo() and then getInfor2(), which is defined in WCFLib/ServicedClass.cs, five times using loop, in mail().
In the above output, first creates new object and then generate new ID before execute getInfo(). ID. After this method execution, the current object will release and creates new object, before we execute getInfo2(). Now again getInfo() will execute with the same object and then, it will release the object and creates new object for getInfo2(). Same process will continue for five times.
Object is releasing after every execution of getInfo2(), because of the following operation behavior in class, WCFLib/ServicedClass.cs, getInfo().
[OperationBehavior(ReleaseInstanceMode=ReleaseInstanceMode.AfterCall)]
1 comment
1 Comment so far
Leave a reply



Release Instance using WCF …
You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…