Streaming binary data to WCF rest service gives Bad Request (400) when content length is greater than 64k

Posted by Mikey Cee on Stack Overflow See other posts from Stack Overflow or by Mikey Cee
Published on 2011-01-10T02:47:04Z Indexed on 2011/01/10 2:53 UTC
Read the original article Hit count: 313

Filed under:
|
|

I have a WCF service that takes a stream:

[ServiceContract]
public class UploadService : BaseService
{
    [OperationContract]
    [WebInvoke(BodyStyle=WebMessageBodyStyle.Bare, Method=WebRequestMethods.Http.Post)]
    public void Upload(Stream data)
    {
        // etc.         
    }
}

This method is to allow my Silverlight application to upload large binary files, the easiest way being to craft the HTTP request by hand from the client. Here is the code in the Silverlight client that does this:

const int contentLength = 64 * 1024;  // 64 Kb

var request = (HttpWebRequest)WebRequest.Create("http://localhost:8732/UploadService/");
request.AllowWriteStreamBuffering = false;
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/octet-stream";
request.ContentLength = contentLength;
using (var outputStream = request.GetRequestStream())
{
     outputStream.Write(new byte[contentLength], 0, contentLength);
     outputStream.Flush();
     using (var response = request.GetResponse());
}

Now, in the case above, where I am streaming 64 kB of data (or less), this works OK and if I set a breakpoint in my WCF method, and I can examine the stream and see 64 kB worth of zeros - yay!

The problem arises if I send anything more than 64 kB of data, for instance by changing the first line of my client code to the following:

const int contentLength = 64 * 1024 + 1; // 64 kB + 1 B

This now throws an exception when I call request.GetResponse():

The remote server returned an error: (400) Bad Request.

In my WCF configuration I have set maxReceivedMessageSize, maxBufferSize and maxBufferPoolSize to 2147483647, but to no avail. Here are the relevant sections from my service's app.config:

<service name="UploadService">
    <endpoint address="" 
              binding="webHttpBinding" 
              bindingName="StreamedRequestWebBinding"
              contract="UploadService" 
              behaviorConfiguration="webBehavior">
        <identity>
            <dns value="localhost" />
        </identity>
    </endpoint>
    <host>
        <baseAddresses>
            <add baseAddress="http://localhost:8732/UploadService/" />
       </baseAddresses>
    </host>       
</service>

<bindings>
    <webHttpBinding>
        <binding name="StreamedRequestWebBinding" 
                 bypassProxyOnLocal="true"
                 useDefaultWebProxy="false"
                 hostNameComparisonMode="WeakWildcard"
                 sendTimeout="00:05:00"
                 openTimeout="00:05:00"
                 receiveTimeout="00:05:00"
                 maxReceivedMessageSize="2147483647"
                 maxBufferSize="2147483647"
                 maxBufferPoolSize="2147483647"
                 transferMode="StreamedRequest">
            <readerQuotas maxArrayLength="2147483647"
                          maxStringContentLength="2147483647" />
        </binding>
    </webHttpBinding>
</bindings>

<behaviors>
    <endpointBehaviors>
        <behavior name="webBehavior">
            <webHttp />
        </behavior>
    <endpointBehaviors>
</behaviors>

How do I make my service accept more than 64 kB of streamed post data?

© Stack Overflow or respective owner

Related posts about wcf

Related posts about rest