title: Download Pdf File with WCF
body: <p>I had to create a WCF service (.net 4.5) that permit to download a pdf, not in REST mode.</p>
  
  <p>I define the interface in this manner</p>
  
  <pre><code>[ServiceContract]
  public interface IService1
  {
      [OperationContract]
      Stream GetPdfFile();
  }
  </code></pre>
  
  <p>And implement implement the GetPdfFile in this manner</p>
  
  <pre><code>    public Stream GetPdfFile()
      {
          Stream ret = null;
          try
          {
              string downloadFilePath = @"C:\Users\jjkdk\Desktop\WTI_PETERS.pdf";
              string fileName = downloadFilePath.Substring(downloadFilePath.LastIndexOf(@"\") + 1);
              String headerInfo = "attachment; filename=" + fileName;
              WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfo;
  
              WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
  
              ret = File.OpenRead(downloadFilePath);
          }
          catch (Exception ex)
          {
              throw ex;
          }
          return ret;
      }
  </code></pre>
  
  <p>The Service tags in the app.config are the follow:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="WCFAperturaAllegatiCrm.Service1"&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress = "http://localhost:8733/Design_Time_Addresses/WCFAperturaAllegatiCrm/Service1/" /&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
          &lt;endpoint address="" binding="basicHttpBinding" contract="WCFAperturaAllegatiCrm.IService1"&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;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior&gt;
            &lt;serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="False" /&gt;          
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Then I created a console client, Run the WCF above, added a Service Reference to the url service and inserted in the console cliente the following code:</p>
  
  <pre><code>SrvAperturaAllegati.Service1Client srv = new SrvAperturaAllegati.Service1Client();
  Stream stream = srv.GetPdfFile();
  Console.WriteLine();
  </code></pre>
  
  <p>I receive the following exception:</p>
  
  <blockquote>
    <p>System.ServiceModel.ProtocolException: The content type text/html; charset=utf->8 of the response message does not match the content type of the binding >(application/soap+xml; charset=utf-8). If using a custom encoder, be sure that >the IsContentTypeSupported method is implemented properly. The first 1024 bytes >of the response were: ........</p>
  </blockquote>
  
  <p>I'm struggling without result. </p>
  
  <p>Could anyone help me?</p>