Web Api Request Throws "Error while copying content to a stream."
Just a guess, should post as comment but I want include a code snippet:
Maybe you call Post
function inside a using
block, but don't use await
.
using (HttpRequestMessage request = ...)
{
// Maybe you use this:
Post(request);
// Instead of this
var response = await Post(request);
}
Or you don't dispose old connects properly.
Also, try add HttpVersion.Version10
to your request, which change header request from Connection: keep-alive
to Connection: close
, which can cause exception in some case you reuse a host (Search for more info)
request.Version = HttpVersion.Version10;
var jsonString = await request.Content.ReadAsStringAsync();
Because the controller's ActionFilterAttribute's
OnActionExecuting
method is calling ReadAsStreamAsync
, the Content can't be read again. I changed ReadAsStreamAsync
to ReadAsStringAsync
and the request's Content is available in the controller. Apparantly, ReadAsStringAsync buffers the Content so it's still available. This link provided the answer.