Skip to content Skip to sidebar Skip to footer

Wcf Rest Service 400 Bad Request

I have WCF RESTful service and Android client. Server replies with 400 when I do bigger request. It seems that I have 65k limit issue like in here or in other million posts on same

Solution 1:

Forgive me if you've seen this link (Similar StackOverflow Question):

By default the WCF Transport is limited to sending messages at 65K. If you want to send larger you need to enable Streaming Transfer Mode and you need to increase the size of MaxReceivedMessageSize, which is there just as a guard to prevent someone killing your server by uploading a massive file.

So, you can do this using binding configuration or you can do it in code. Here is one way to do it in code:

var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]); //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;  // Allow files up to 10MB

Solution 2:

You don't need to use streaming in this case - all you need to do is to increase the maxReceivedMessageSize quota on the standard webHttpEndpoint:

<standardEndpoints><webHttpEndpoint><!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
    --><standardEndpointname=""helpEnabled="true"automaticFormatSelectionEnabled="true"maxReceivedMessageSize="1000000"/></webHttpEndpoint></standardEndpoints>

Update: if the config change didn't work (I don't know why), you can try increasing it in code. By using a custom service host factory, you get a reference to the endpoint object and you can increase the quota there. The code below shows one such a factory (you'll need to update the RegisterRoute code to use this new factory):

publicclassMyWebServiceHostFactory : ServiceHostFactory
{
    protectedoverride ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        returnbase.CreateServiceHost(serviceType, baseAddresses);
    }

    classMyWebServiceHost : WebServiceHost
    {
        publicMyWebServiceHost(Type serviceType, Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }
        protectedoverridevoidOnOpening()
        {
            base.OnOpening();
            foreach (ServiceEndpoint endpoint inthis.Description.Endpoints)
            {
                if (!endpoint.IsSystemEndpoint)
                {
                    Binding binding = endpoint.Binding;
                    if (binding is WebHttpBinding)
                    {
                        ((WebHttpBinding)binding).MaxReceivedMessageSize = 1000000;
                    }
                    else
                    {
                        CustomBinding custom = binding as CustomBinding;
                        if (custom == null)
                        {
                            custom = new CustomBinding(binding);
                        }

                        custom.Elements.Find<HttpTransportBindingElement>().MaxReceivedMessageSize = 1000000;
                    }
                }
            }
        }
    }
}

Post a Comment for "Wcf Rest Service 400 Bad Request"