title: When is destructor called in a WCF service
body: <p>I need to create a service which will maintain a WCF session.
  In the constructor I read in data from the DB and when the session ends I have to save it back.</p>
  
  <p>If I understand correctly the session ends when I call Close() on the Client (My client ServiceClient was created with SvcUtil.exe).</p>
  
  <p>When I test it I see that it is sometimes called after approx. 10 minutes, sometimes after 20 minutes and sometimes not at all.  </p>
  
  <p>So when is the destructor called?</p>
  
  <p>Service</p>
  
  <pre><code>   [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
     public class Service:IService
     {
       private User m_User = null;
  
       public  Service()
       {
         m_User = User.LoadFromDB();
       }
  
       ~Service()
       {
         m_User.SaveToDB();
       }
  
       public void SetName(string p_Name)
       {
         m_User.Name = p_Name;
       }
      }
  </code></pre>
  
  <p>Web.config</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;system.web&gt;
      &lt;sessionState timeout="2" /&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
        &lt;services&gt;
          &lt;service name="Karatasi.Services.B2C"  behaviorConfiguration="ServiceBehavior"&gt;
            &lt;host&gt;
              &lt;baseAddresses&gt;
                &lt;add baseAddress="http://localhost:19401/B2C.svc"/&gt;
              &lt;/baseAddresses&gt;
            &lt;/host&gt;
          &lt;endpoint
             address=""
             binding="wsHttpBinding"
             bindingConfiguration="test"
             contract="Karatasi.Services.IB2C"
           /&gt;
          &lt;endpoint
             address="mex"
             binding="mexHttpBinding"
             contract="IMetadataExchange"
           /&gt;
         &lt;/service&gt;
       &lt;/services&gt;
     &lt;bindings&gt;
       &lt;wsHttpBinding&gt;
         &lt;binding name="test" receiveTimeout="00:01:00" &gt;
           &lt;reliableSession enabled="true" ordered="false" inactivityTimeout="00:01:00"/&gt;
         &lt;/binding&gt;
       &lt;/wsHttpBinding&gt;
      &lt;/bindings&gt;
    &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
        &lt;behavior name="ServiceBehavior"&gt;
          &lt;serviceMetadata httpGetEnabled="true" /&gt;
          &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
        &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
    &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Client</p>
  
  <pre><code>    ServiceClient serviceClient = null;
      try
      {
        serviceClient = new ServiceClient();
        serviceClient.SetName("NewName");
        Console.WriteLine("Name set");
      }
      catch (Exception p_Exc)
      {
        Console.WriteLine(p_Exc.Message);
      }
      finally
      {
        if (serviceClient != null)
        {
          if (serviceClient.State == CommunicationState.Faulted)
          {
            serviceClient.Abort();
          }
          else
          {
            serviceClient.Close();
          }
        }
        Console.ReadKey();
      }
  </code></pre>