简体   繁体   English

WCF服务:MaxReceivedMessageSize(异步)

[英]WCF Service: MaxReceivedMessageSize (async)

I ran into an exception because the message size and/or array-length of data that I'm transmitting from a WCF server to a client exceeds the maximum size. 我遇到了一个异常,因为我从WCF服务器传输到客户端的消息大小和/或数据的数组长度超过了最大大小。 This topic occurs quite often, but most solutions are about changing the MaxReceivedMessageSize of the service binding. 这个主题经常发生,但是大多数解决方案都是关于更改服务绑定的MaxReceivedMessageSize。

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

But I'm interested how this can be resolved in a different way. 但我对如何以其他方式解决此问题感兴趣。 Consider the following method. 考虑以下方法。 It might result in a large byte array. 这可能会导致一个大字节数组。 I also tried converting the image into a Base64String. 我还尝试将图像转换为Base64String。

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

Is there a common pattern to use BeginRead/EndRead for streaming the byte array in smaller chunks to the client? 是否存在使用BeginRead / EndRead将字节数组以较小的块流式传输到客户端的通用模式? How would you do that in code? 您将如何用代码做到这一点?

According to Microsoft (see this link ) the contract has to be adjusted and the end-point configuration as well. 根据Microsoft的说法(请参阅此链接 ),必须调整合同,还必须调整端点配置。

<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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM