2

When I use the the ObjectContent object to create the HttpContent to send a request via HttpClient to a Web API service I am getting the following error:

Cannot write more bytes to the buffer than the configured maximum buffer size: 65536

The following code is being used to send the request. The Card object has about 15 properties.

var client = new HttpClient();
var content = new ObjectContent<IEnumerable<Card>>(cards, "application/xml");
MessageBox.Show(content.ReadAsString());  //This line gives me the same error.

var response = client.Post("http://localhost:9767/api/cards", content);

How do I change the configured size to something greater than 65,536?

Dean
  • 1,550
  • 2
  • 15
  • 22

3 Answers3

2

Since the problem resides in the ReadAsString extension method I would suggest that you create your own extension method to solve the maximum buffer size issue.

Here’s an example of a ReadAsLargeString extension method that maybe solves the problem.

public static string ReadAsLargeString(this HttpContent content)
{
    var bufferedContent = new MemoryStream();
    content.CopyTo(bufferedContent);

    if (bufferedContent.Length == 0)
    {
        return string.Empty;
    }

    Encoding encoding = DefaultStringEncoding;
    if ((content.Headers.ContentType != null) && (content.Headers.ContentType.CharSet != null))
    {
        encoding = Encoding.GetEncoding(content.Headers.ContentType.CharSet);
    }

    return encoding.GetString(bufferedContent.GetBuffer(), 0, (int)bufferedContent.Length);
}
hskan
  • 683
  • 6
  • 7
  • I'm not quite sure how this helps. How would I "tell" the HttpClient to use this method? – Dean Nov 04 '11 at 14:42
  • When you have implemented the extension method try with content.ReadAsLargeString() – hskan Nov 04 '11 at 14:59
  • The post method of HttpClient accepts an HttpContent reference pointer, not a string. – Dean Nov 04 '11 at 15:04
  • My apology for not reading the question thorough enough. Have you tried? HttpClient client = new HttpClient(new HttpClientHandler { MaxRequestContentBufferSize = int.MaxValue }); client.MaxResponseContentBufferSize = int.MaxValue; – hskan Nov 07 '11 at 07:55
1

There is a thread about this. Try using HttpCompletionOption.ResponseContentRead:

var message = new HttpRequestMessage(HttpMethod.Post, "http://localhost:9767/api/cards");
message.Content = content; 
var client = new HttpClient();
client.Send(message, HttpCompletionOption.ResponseContentRead);
bkaid
  • 51,465
  • 22
  • 112
  • 128
  • If I add MessageBox.Show(content.ReadAsString()); immediately following the var content = ... line it gives me the same error. – Dean Nov 02 '11 at 16:37
  • I updated the answer. You may need to look at the source to see why its doing that. – bkaid Nov 02 '11 at 17:20
0

Try this for the client:

HttpClient client = new HttpClient("http://localhost:52046/");

// enable support for content up to 10 MB size
HttpClientChannel channel = new HttpClientChannel() {
    MaxRequestContentBufferSize = 1024 * 1024 * 10 
};

client.Channel = channel;

On the Server (snippet is based preview 4 but you should get the clue):

public class CustomServiceHostFactory : HttpConfigurableServiceHostFactory {
    public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses) {
        var host = base.CreateServiceHost(constructorString, baseAddresses);

        foreach (HttpEndpoint endpoint in host.Description.Endpoints) {
            endpoint.TransferMode = TransferMode.Streamed;
            endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;
        }

        return host;
    }
}
Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124