开发者

How to set the MaxReceivedMessageSize programmatically when using a WCF Client?

开发者 https://www.devze.com 2022-12-23 06:43 出处:网络
I want to set the MaxReceivedMessageSize property to some higher limit (Due to (400) Bad Request error) in my client programmatically.

I want to set the MaxReceivedMessageSize property to some higher limit (Due to (400) Bad Request error) in my client programmatically. This is the code I am using...

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

My service url is dynamic and hence cannot use the web.config.

//The following code doesn't seem to take effect
((WSHttpBinding)wcfClient.Channel开发者_运维百科Factory.Endpoint.Binding)
        .MaxReceivedMessageSize = 2147483647;

What am I doing wrong?


Have you tried re-ordering the calls so that you set the MaxReceivedMessageSize before instantiating the client? eg,

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

This may or may not help your 400 error, though.


I had similar problems in my wcf-service and I solved it with:

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


This works well, though it's not that obvious. It retains all the existing binding properties and only adjusts the MaxReceivedMessageSize (which, incidentally, also increases MaxBufferSize to the same size).

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

精彩评论

暂无评论...
验证码 换一张
取 消