我的目标是将PDF文件流返回给客户端。WCF流式PDF响应
所以在WCF身边,我有:
public interface IPersonalPropertyService
{
[OperationContract]
Stream GetQuotation();
}
public class PersonalPropertyService : IPersonalPropertyService
{
public Stream GetQuotation()
{
var filePath = HostingEnvironment.ApplicationPhysicalPath + @"Quotation.pdf";
var fileInfo = new FileInfo(filePath);
// check if exists
if (!fileInfo.Exists)
throw new FileNotFoundException("File not found");
FileStream stm = File.Open(filePath, FileMode.Open);
WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
return stm;
}
}
配置部分如下:
<system.serviceModel>
<client>
<endpoint
binding="basicHttpBinding"
bindingConfiguration="StreamedHttp"
contract="IPersonalPropertyService" >
</endpoint>
</client>
<bindings>
<basicHttpBinding>
<binding name="StreamedHttp" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="Streamed">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
现在在客户端(如控制台应用程序),当我创建服务引用,我期望看到我的basicHttpBinding StreamedHttp配置,但生成以下配置:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_PersonalPropertyService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="MyPath/PersonalPropertyService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_PersonalPropertyService"
contract="TheNamespace.ServiceReference.PersonalPropertyService"
name="BasicHttpBinding_PersonalPropertyService" />
</client>
</system.serviceModel>
,我估计是因为这个原因,我得到一个异常的ProtocolException说
响应消息的内容类型application/PDF格式不匹配 内容类型的绑定(文本/ XML的;字符集= UTF-8)。
如何强制客户端接受WCF端定义的流式配置?
谢谢
当你在浏览器中输入“http://....../ GetQuotation”时,你会得到什么? – Eser
我改变了web.config与浏览器进行交互,现在当我执行http://..../PersonalPropertyService.svc/GetQuotation时,我得到:方法不允许。 – Nostradamus
我从来没有能够读取WCF conf文件,但如果您有兴趣,我可以发布一个基于WebServiceHost的自托管WCF服务器,返回文件 – Eser