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?