我的目標是將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