daniele3004
daniele3004

Reputation: 13960

Send Big Data to WCF - (413) Request Entity Too

I have a WCF server,

when I connect the client(WinForm) I set by code the binding parameter with this code:

String HTTP_SERVER = http:\\.......

private static BasicHttpBinding getBinding()
{
    //WSHttpBinding binding = new WSHttpBinding();
    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);

    binding.TextEncoding = System.Text.Encoding.UTF8;
    binding.ReaderQuotas.MaxArrayLength = int.MaxValue;

    binding.ReceiveTimeout =new TimeSpan(8, 0,0);
    binding.SendTimeout = new TimeSpan(8, 0, 0);

    binding.MaxReceivedMessageSize = int.MaxValue;
    binding.MaxBufferSize = int.MaxValue;
    binding.MaxBufferPoolSize = int.MaxValue;


    binding.ReaderQuotas.MaxDepth = 64;
    binding.ReaderQuotas.MaxArrayLength= int.MaxValue;
    binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;

    return binding;

}

ConnectionToServer = new ConnectionToServer (getBinding(), new EndpointAddress(HTTP_SERVER));

This code run correctly but now I need to send a very big data in Array and when I try to send a big array I have this error:

(413) Request Entity Too

I need to configure this connection by code and not by xml.

I have foud example to solve this problen only by xml but I need to set by c# code

Is necessary to set any parameter in web.config(WCF server side)?

Upvotes: 1

Views: 824

Answers (1)

Jorge Del Conde
Jorge Del Conde

Reputation: 361

If this is on the client, you can add the following behavior to your channelFactory:

public class MaxItemsInGraphBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        foreach (OperationDescription operation in endpoint.Contract.Operations)
        {
            var dc = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
            if (dc != null)
            {
                dc.MaxItemsInObjectGraph = int.MaxValue;
            }
        }
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

Upvotes: 1

Related Questions