Matthias
Matthias

Reputation: 5764

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. This topic occurs quite often, but most solutions are about changing the MaxReceivedMessageSize of the service binding.

    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.

[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? How would you do that in code?

Upvotes: 0

Views: 185

Answers (1)

Matthias
Matthias

Reputation: 5764

According to Microsoft (see this link) the contract has to be adjusted and the end-point configuration as well.

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

Upvotes: 1

Related Questions