Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wcf

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.ChannelFactory.Endpoint.Binding)
        .MaxReceivedMessageSize = 2147483647;

What am I doing wrong?

like image 915
Pratt Avatar asked Mar 16 '10 19:03

Pratt


People also ask

What is default MaxReceivedMessageSize?

You need to set the MaxReceivedMessageSize attribute in your binding configuration. By default, it is 65536.

What is the maximum value for MaxReceivedMessageSize?

MaxReceivedMessageSize = 1000000; The value of this property can also be set in the configuration file.


2 Answers

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.

like image 142
Cheeso Avatar answered Sep 28 '22 11:09

Cheeso


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));
like image 33
Martin Avatar answered Sep 28 '22 10:09

Martin