【发布时间】:2017-01-14 01:10:01
【问题描述】:
我必须创建一个允许下载 pdf 的 WCF 服务 (.net 4.5),而不是在 REST 模式下。
我是这样定义接口的
[ServiceContract]
public interface IService1
{
[OperationContract]
Stream GetPdfFile();
}
并以这种方式实现GetPdfFile
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;
}
app.config 中的 Service 标签如下:
<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>
然后我创建了一个控制台客户端,运行上面的 WCF,向 url 服务添加一个服务引用,并在控制台客户端中插入以下代码:
SrvAperturaAllegati.Service1Client srv = new SrvAperturaAllegati.Service1Client();
Stream stream = srv.GetPdfFile();
Console.WriteLine();
我收到以下异常:
System.ServiceModel.ProtocolException:内容类型text/html;响应消息的charset=utf->8 与绑定的内容类型不匹配>(application/soap+xml; charset=utf-8)。如果使用自定义编码器,请确保 >IsContentTypeSupported 方法已正确实现。响应的前 1024 个字节是:........
我挣扎着没有结果。
谁能帮帮我?
【问题讨论】:
-
阅读这个类似的 SO 问题:stackoverflow.com/questions/19549423/…