2013-07-25 170 views
0

我遇到了一個異常,因爲我從WCF服務器發送到客戶端的數據的消息大小和/或數組長度超過了最大大小。本主題經常發生,但大多數解決方案都是關於更改服務綁定的MaxReceivedMessageSize。WCF服務:MaxReceivedMessageSize(異步)

var binding = new WSHttpBinding { MaxReceivedMessageSize = int.MaxValue }; 
    binding.ReaderQuotas.MaxArrayLength = int.MaxValue; 
    host.AddServiceEndpoint(typeof(IEditor), binding, uri); 

但我很感興趣如何以不同的方式解決這個問題。考慮以下方法。它可能會導致一個大的字節數組。我也嘗試將圖像轉換成Base64String。

[ServiceContract] 
public interface IEditor 
{ 
    [OperationContract] 
    byte[] GetImage(); 
} 

是否有使用的BeginRead/EndRead的流字節數組較小的塊給客戶一個共同的模式?你會如何在代碼中做到這一點?

回答

1

根據微軟(見this link)合同必須進行調整和終點配置。

<system.serviceModel> 
    … 
    <bindings> 
     <basicHttpBinding> 
     <binding name="ExampleBinding" transferMode="Streaming"/> 
     </basicHttpBinding> 
    </bindings> 
    … 
<system.serviceModel> 

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] 
public interface IStreamedService 
{ 
    [OperationContract] 
    Stream Echo(Stream data); 
    [OperationContract] 
    Stream RequestInfo(string query); 
    [OperationContract(OneWay=true)] 
    void ProvideInfo(Stream data); 
}