19

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pratt
  • 247
  • 1
  • 3
  • 5

3 Answers3

26

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.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
11

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));
Martin
  • 111
  • 1
  • 4
1

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
SteveCinq
  • 1,920
  • 1
  • 17
  • 22