【问题标题】:How to set the MaxReceivedMessageSize programmatically when using a WCF Client?使用 WCF 客户端时如何以编程方式设置 MaxReceivedMessageSize?
【发布时间】:2011-01-28 06:46:29
【问题描述】:

我想以编程方式在我的客户端中将 MaxReceivedMessageSize 属性设置为更高的限制(由于 (400) Bad Request 错误)。 这是我正在使用的代码...

WCFServiceTestClient wcfClient = 
    new WCFServiceTestClient(new wsHttpBinding(), strServiceURL);

我的服务 url 是动态的,因此无法使用 web.config。

//The following code doesn't seem to take effect
((WSHttpBinding)wcfClient.ChannelFactory.Endpoint.Binding)
        .MaxReceivedMessageSize = 2147483647;

我做错了什么?

【问题讨论】:

    标签: wcf


    【解决方案1】:

    您是否尝试过重新排序调用以便在实例化客户端之前设置 MaxReceivedMessageSize?例如,

    var binding = new wsHttpBinding();
    binding.MaxReceivedMessageSize = Int32.MaxValue; 
    var wcfClient = new WCFServiceTestClient(binding, strServiceURL); 
    

    不过,这可能对您的 400 错误有所帮助,也可能无济于事。

    【讨论】:

      【解决方案2】:

      我在我的 wcf 服务中遇到了类似的问题,我通过以下方式解决了它:

      CustomBinding binding = (CustomBinding)PDAServiceContractClient.CreateDefaultBinding();
                  HttpTransportBindingElement httpBindingElement = new HttpTransportBindingElement();
                  httpBindingElement.MaxBufferSize = Int32.MaxValue;
                  httpBindingElement.MaxReceivedMessageSize = Int32.MaxValue;
                  binding.Elements.Add(httpBindingElement);
      
                  string address = PDAServiceContractClient.EndpointAddress.Uri.ToString();
                  m_proxy = new PDAServiceContractClient(binding, new EndpointAddress(address));
      

      【讨论】:

        【解决方案3】:

        这很好用,虽然不是很明显。它保留了所有现有的绑定属性,并且只调整了MaxReceivedMessageSize(顺便说一下,这也将MaxBufferSize 增加到相同的大小)。

        Dim oClient as WcfClient = New WcfClient
        CType(oClient.Endpoint.Binding, ServiceModel.BasicHttpBinding).MaxReceivedMessageSize = Int32.MaxValue
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-11
          • 2017-03-30
          • 1970-01-01
          • 1970-01-01
          • 2012-06-15
          • 2011-02-25
          相关资源
          最近更新 更多