InstanceContextMode.PerSession not working in IIS hosted service

I have simple wcf service hosted in IIS 7. The fist call to the service (GetData) creates the Transaction object. I expected that the 2nd call to the service (GetData2) would have the Transaction object available. However the value of MyTrans is null on the 2nd call.   I thought that     [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]  would result in this object being retained for the session. Any idea what I am doing wrong ?


Service code:
   [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
   public class Service1 : IService1
        Transaction MyTrans;
        public Transaction GetData(string value)
            MyTrans = new Transaction();
            MyTrans.companyId="CCS Ltd";
            MyTrans.trans = "Initmenu01";
            MyTrans.userId = "PClarkeirl";
            MyTrans.curSeq = 123;
            return MyTrans;

        public Transaction GetData2(string value)
            MyTrans.nextSeq = MyTrans.nextSeq+1;
            return MyTrans;

Client code:

                ServiceReference2.Service1Client client = new ServiceReference2.Service1Client();
                ServiceReference2.Transaction MyClientTrans = client.GetData("XXX");
                MyClientTrans = client.GetData2("XXX");

web.config source
<?xml version="1.0" encoding="utf-8" ?>

    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <compilation debug="true" />
  <!-- When deploying the service library project, the content of the config file must be added to the host's
  app.config file. System.Configuration does not support config files for libraries. -->
      <service name="WcfServiceLibrary7.Service1">
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary7/Service1/" />
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary7.IService1">
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
            <dns value="localhost"/>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          <!-- 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" />

Who is Participating?
pclarke7Author Commented:
Dissapointing that there were no answers received for this question,
however for anyone reading this I figured out why the perSession was not working. It seems that perSession is not handled by BasicHttpBinding. I have to change the binding to WsHttpBinding. Once I did this then the data was retained.

pclarke7Author Commented:
Sorted this one out myself as there was zero response to the question
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.