title: Common WCF Exception : Connection Unexpectedly Closed
body: <p>I have three projects.  One is a WCF Services Project, one is a WPF Project, and one is a Microsoft Unit Testing Project.  I setup the WCF Services project with a data object that looks like this:</p>
  
  <pre><code>[DataContract]
  public enum Priority
  {
      Low,
      Medium,
      High
  }
  
  [DataContract]
  public struct TimeInfo
  {
      [DataMember]
      public Int16 EstimatedHours { get; set; }
  
      [DataMember]
      public Int16 ActualHours { get; set; }
  
      [DataMember]
      public DateTime StartDate { get; set; }
  
      [DataMember]
      public DateTime EndDate { get; set; }
  
      [DataMember]
      public DateTime CompletionDate { get; set; }
  }
  
  [DataContract]
  public class Task
  {
      [DataMember]
      public string Title { get; set; }
  
      [DataMember]
      public string Description { get; set; }
  
      [DataMember]
      public Priority Priority { get; set; }
  
      [DataMember]
      public TimeInfo TimeInformation { get; set; }
  
      [DataMember]
      public Decimal Cost { get; set; }
  }
  </code></pre>
  
  <p>My contract looks like this:</p>
  
  <pre><code>[ServiceContract]
  public interface ITaskManagement
  {
      [OperationContract]
      List&lt;Task&gt; GetTasks();
  
      [OperationContract]
      void CreateTask(Task taskToCreate);
  
      [OperationContract]
      void UpdateTask(Task taskToCreate);
  
      [OperationContract]
      void DeleteTask(Task taskToDelete);
  }
  </code></pre>
  
  <p>When I try to use the service in either the WPF Application or the Unit Test Project with this code:</p>
  
  <pre><code>var client = new TaskManagementClient();
  
  textBox1.Text = client.GetTasks().ToString();
  
  client.Close();
  </code></pre>
  
  <p>I get the following error:  "The underlying connection was closed: The connection was closed unexpectedly."</p>
  
  <p>The app.config for the WPF and Unit Test Projects look like this:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;bindings&gt;
          &lt;wsHttpBinding&gt;
              &lt;binding name="WSHttpBinding_ITaskManagement" closeTimeout="00:01:00"
                  openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                  bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                  maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                  messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                  allowCookies="false"&gt;
                  &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                      maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
                  &lt;reliableSession ordered="true" inactivityTimeout="00:10:00"
                      enabled="false" /&gt;
                  &lt;security mode="Message"&gt;
                      &lt;transport clientCredentialType="Windows" proxyCredentialType="None"
                          realm="" /&gt;
                      &lt;message clientCredentialType="Windows" negotiateServiceCredential="true"
                          algorithmSuite="Default" establishSecurityContext="true" /&gt;
                  &lt;/security&gt;
              &lt;/binding&gt;
          &lt;/wsHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;client&gt;
          &lt;endpoint address="http://localhost:9999/TaskManagement.svc"
              binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITaskManagement"
              contract="TaskManagement.ITaskManagement" name="WSHttpBinding_ITaskManagement"&gt;
              &lt;identity&gt;
                  &lt;dns value="localhost" /&gt;
              &lt;/identity&gt;
          &lt;/endpoint&gt;
      &lt;/client&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>and the web.config of the WCF Service looks like this:</p>
  
  <pre><code>    &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
          &lt;serviceBehaviors&gt;
              &lt;behavior name="InternetBasedWcfServices.TaskManagementBehavior"&gt;
                  &lt;serviceMetadata httpGetEnabled="true" /&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
              &lt;/behavior&gt;
              &lt;behavior name="InternetBasedWcfServices.ScheduleManagementBehavior"&gt;
                  &lt;serviceMetadata httpGetEnabled="true" /&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
              &lt;/behavior&gt;
          &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;services&gt;
          &lt;service behaviorConfiguration="InternetBasedWcfServices.TaskManagementBehavior"
              name="InternetBasedWcfServices.TaskManagement"&gt;
              &lt;endpoint address="" binding="wsHttpBinding" contract="InternetBasedWcfServices.ITaskManagement"&gt;
                  &lt;identity&gt;
                      &lt;dns value="localhost" /&gt;
                  &lt;/identity&gt;
              &lt;/endpoint&gt;
              &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
          &lt;/service&gt;
          &lt;service behaviorConfiguration="InternetBasedWcfServices.ScheduleManagementBehavior"
              name="InternetBasedWcfServices.ScheduleManagement"&gt;
              &lt;endpoint address="" binding="wsHttpBinding" contract="InternetBasedWcfServices.IScheduleManagement"&gt;
                  &lt;identity&gt;
                      &lt;dns value="localhost" /&gt;
                  &lt;/identity&gt;
              &lt;/endpoint&gt;
              &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
          &lt;/service&gt;
      &lt;/services&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>This is not the first time this has happened, and I'm guessing it is a configuration issue.  But each time I've usually just blown away my service and put it back or created a new service project.  Then everything works wonderfully.  If anyone has any ideas, that would be awesome.  Thx.</p>
  
  <p>**</p>
  
  <blockquote>
    <p>Updated:  I've added comments for more
    of my troubleshooting on this problem.
    When an answer is available, if the
    answer is unpublished, I'll add it as
    an official "answer".</p>
  </blockquote>
  
  <p>**</p>