I'm new to WCF and I wrote a sample WCF service which uses the InstanceContextMode. When I'm using the PerSession, my counter value doesn't gets incremented. Why doesn't it uses the same instance for every service call. Below is my code
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service1 : IService1
{
int count;
public int Add(int a, int b)
{
count++;
return a + b;
}
public int GetCount()
{
return count;
}
}
WebConfig
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
Client.cs
DemoWCFService.Service1Client client = new DemoWCFService.Service1Client();
Console.WriteLine(""+ client.Add(10,20));
Console.WriteLine("" + client.Add(10, 20));
Console.WriteLine("" + client.Add(10, 20));
Console.WriteLine("" + client.GetCount());
Console.ReadKey();
Please help me with this.