Showing posts with label Instance Management in WCF. Show all posts
Showing posts with label Instance Management in WCF. Show all posts

Tuesday, 10 September 2013

Instance Management in WCF

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>