Submit Big Data to WCF - (413) Request an Entity

I have a WCF server,

when I connect the client (WinForm), I set 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 works correctly, but now I need to send very large data to Array, and when I try to send a large array, I have this error:

(413) It is too difficult to query an entity

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

I have an example to solve this problem only with xml, but I need to install C # code

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

+1
source share
1 answer

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

 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) { } } 
+1
source

Source: https://habr.com/ru/post/1206125/


All Articles