1

I had to create a WCF service (.net 4.5) that permit to download a pdf, not in REST mode.

I define the interface in this manner

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Stream GetPdfFile();
}

And implement implement the GetPdfFile in this manner

    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;
    }

The Service tags in the app.config are the follow:

<system.serviceModel>
    <services>
      <service name="WCFAperturaAllegatiCrm.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WCFAperturaAllegatiCrm/Service1/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WCFAperturaAllegatiCrm.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />          
        </behavior>
      </serviceBehaviors>
    </behaviors>
</system.serviceModel>

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:

SrvAperturaAllegati.Service1Client srv = new SrvAperturaAllegati.Service1Client();
Stream stream = srv.GetPdfFile();
Console.WriteLine();

I receive the following exception:

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: ........

I'm struggling without result.

Could anyone help me?

LarAnto
  • 136
  • 1
  • 13
  • Read this similar SO question: http://stackoverflow.com/questions/19549423/return-pdf-file-from-wcf-service?rq=1 – tgolisch Sep 06 '16 at 15:43

1 Answers1

0

This issue coming from

....
WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfo;
WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
...

In case you want to use this service as SOAP service, you can't add OutgoingResponse.Headers and OutgoingResponse.ContentType. Comment this line, and it it will work fine in SOAP. This lines is for REST services.

Corrected code here:

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;
}