C# 也向WCF-(413)请求实体发送大数据

C# 也向WCF-(413)请求实体发送大数据,c#,winforms,wcf,C#,Winforms,Wcf,我有一个WCF服务器 当我连接客户端(WinForm)时,我通过代码设置绑定参数,代码如下: String HTTP_SERVER = http:\\....... private static BasicHttpBinding getBinding() { //WSHttpBinding binding = new WSHttpBinding(); BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurit

我有一个WCF服务器

当我连接客户端(WinForm)时,我通过代码设置绑定参数,代码如下:

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));
这段代码运行正常,但现在我需要在数组中发送一个非常大的数据,当我尝试发送一个大数组时,出现以下错误:

(413)也请求实体

我需要通过代码而不是xml来配置此连接

我已经提供了一个仅用xml解决这个问题的示例,但我需要用c#代码进行设置


是否需要在web.config(WCF服务器端)中设置任何参数?

如果这是在客户端上,则可以向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)
    {
    }
}
公共类MaxItemsInGraphBehavior:IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint端点、BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint端点、ClientRuntime ClientRuntime)
{
foreach(endpoint.Contract.Operations中的OperationDescription操作)
{
var dc=operation.Behaviors.Find();
如果(dc!=null)
{
dc.MaxItemsInObjectGraph=int.MaxValue;
}
}
}
公共无效ApplyDispatchBehavior(ServiceEndpoint端点、EndpointDispatcher端点Dispatcher)
{
}
公共void验证(ServiceEndpoint)
{
}
}

没有回答您的问题?我尝试了,但没有运行@strickt01