title: WCF instancing mode Per Session not working as expected
body: <p>I'm writing a WCF application that generates sets of data, and allows the client to create them, navigate through them, and retrieve data from them.  The data is stored in static variables of the service class.  Particuarly, they're stored in a dictionary, using GUID strings as the keys.</p>
  
  <p>I'm setting the instancing mode of the service class to PerSession, which as far as I know should work.  The client calls a function called CreateRecordSet, which adds an entry to the dictionary and returns the key string.  The client then calls a function called First(), which should retrieve the first record in the data set.  However, when it attempts this, the dictionary no longer contains any entries.</p>
  
  <p>Strangely enough, if I set the instancing mode to Single, it works fine.  But, if I set it to PerSession, it seems to lose the dictionary between the two calls, which I would expect if I had set it to PerCall.</p>
  
  <p>Here's the interface definition:</p>
  
  <pre><code>namespace OrsonServiceLibrary
  {
      [ServiceContract]
      public interface IService1
      {
  
          [OperationContract]
          string CreateRecordSet();
  
          [OperationContract]
          object First(string setkey);
  
          // Removed unused methods
  
      }
  
      [DataContract]
      [KnownType(typeof(JMMCustomer))]
      public class JMMCustomer
      {
          [DataMember]
          public string CUSTFNAME { get; set; }
          [DataMember]
          public string CUSTLNAME { get; set; }
          [DataMember]
          public string CUSTADDRESS { get; set; }
          [DataMember]
          public string CUSTCITY { get; set; }
          [DataMember]
          public string CUSTKEY { get; set; }
  
      }
  }
  </code></pre>
  
  <p>Here's the service class code:</p>
  
  <pre><code>    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
      public class Service1 : IService1
      {
          protected static Dictionary&lt;String,RecordSet&gt; RecordSets;
  
          public Service1()
          {
              RecordSets = new Dictionary&lt;string,RecordSet&gt;();
          }
  
          public string CreateRecordSet()
          {
              List&lt;object&gt; c = new List&lt;object&gt;(GetAllCustomers());
              var rs = new RecordSet(c);
              string thekey = System.Guid.NewGuid().ToString();
  
              RecordSets.Add(thekey, rs);
  
              Console.WriteLine("End of CreateRecordSet().  RecordSets.Count = " + RecordSets.Count);
  
              return thekey;
          }
  
          public object First(string setkey)
          {
              Console.WriteLine("Beginning of First().  RecordSets.Count = " + RecordSets.Count);
  
              if (RecordSets[setkey].thelist.Count &lt; 1)
                  throw new Exception ("No items in the data set.");
              else
                  RecordSets[setkey].cursor = 0;
  
              return RecordSets[setkey].thelist[RecordSets[setkey].cursor];
          }
  
          // Removed unused methods
     }
  </code></pre>
  
  <p>Here's the client code:</p>
  
  <pre><code>static void Main(string[] args)
  {
      Console.WriteLine("Press Enter to begin.");
      Console.ReadLine();
  
      ServiceReference1.Service1Client MyService = new ServiceReference1.Service1Client("Service1");
  
      string sk = MyService.CreateRecordSet();
  
      ServiceReference1.JMMCustomer jc;
      jc = (ServiceReference1.JMMCustomer)MyService.First(sk);
  
      Console.WriteLine(jc.CUSTFNAME + " " + jc.CUSTLNAME);
  
      Console.WriteLine("Press Enter to exit.");
      Console.ReadLine();
  }
  </code></pre>
  
  <p>As you can see, I add some debugging messages to the hosts's console to show the status of the dictionary.  Here's the hosts's output:</p>
  
  <pre><code>Host started.  Press Enter to terminate host.
  End of CreateRecordSet().  RecordSets.Count = 1
  Beginning of First().  RecordSets.Count = 0
  </code></pre>
  
  <p>And then of course, I get an exception in the next line of code in First() when I try to reference RecordSets[setkey] because RecordSets is empty.</p>
  
  <p>If I change the behavior to:</p>
  
  <pre><code>[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
  </code></pre>
  
  <p>...it then works as expected.  The host output becomes:</p>
  
  <pre><code>Host started.  Press Enter to terminate host.
  End of CreateRecordSet().  RecordSets.Count = 1
  Beginning of First().  RecordSets.Count = 1
  </code></pre>
  
  <p>... and then the client prints the customer names as expected, without errors.</p>
  
  <p>So what am I missing?  My understanding is that as long as I use the same ServiceReference (MyService) for both calls, then they should share the same static variables on the server if the server's InstanceContextMode is set to PerSession.  Am I incorrect?</p>
  
  <p>-Joe</p>