Hello,
I am writing owin middleware which needs to read from the http body stream.
This fails miserably when using the middleware in an Asp.net MVC / WebApi project.
Using 5.1.0 it fails silently, but the api controller methods receive null arguments for all methods (as if it receives no body content and cannot bind anything).
in 5.1.1 this gives the following error:
"Unable to read the entity body. A portion of the request stream has already been read."
In 5.1.1 the case where the request stream has already been read has been fixed in this commit: https://github.com/ASP-NET-MVC/aspnetwebstack/commit/3974b68329b7c5c007413bb4f03f44ecc2028bd5
However, whatever I try to reset the request stream from the middleware, it is not working.
Replacing IOwinRequest.Body with a Memorystream / copying it, resetting it:
var bufferedReqStream = new MemoryStream();
await Request.Body.CopyToAsync(bufferedReqStream);
Request.Body = bufferedReqStream;
// other code
bufferedReqStream.Position = 0;
It looks like WebApi is not taking part in the whole owin process or at least it's not handling the body stream well.
My current solution is to stay away from 'GlobalConfiguration.Configure()' and use app.UseWebApi(..) with my own HttpConfiguration. This makes sure WebApi is correctly integrated in the whole owin pipeline.
But this creates a load of other problems because other components (like WebAPi help pages / NInject webapi integration / other 3rd party components) all depend on 'GlobalConfiguration.Configuration'.
Am I doing something wrong? or is WebApi really hijacking the http body stream, failing whenever some other owin middleware tries to access it.
Comments: Can you try the following workaround when using 5.1.1 and see if this fixes your issue: HttpContextBase contextBase = (HttpContextBase)context.Environment["System.Web.HttpContextBase"]; Stream buffreredRequestStream = contextBase.Request.InputStream; // other code buffreredRequestStream.Position = 0; Request.Body = buffreredRequestStream;
I am writing owin middleware which needs to read from the http body stream.
This fails miserably when using the middleware in an Asp.net MVC / WebApi project.
Using 5.1.0 it fails silently, but the api controller methods receive null arguments for all methods (as if it receives no body content and cannot bind anything).
in 5.1.1 this gives the following error:
"Unable to read the entity body. A portion of the request stream has already been read."
In 5.1.1 the case where the request stream has already been read has been fixed in this commit: https://github.com/ASP-NET-MVC/aspnetwebstack/commit/3974b68329b7c5c007413bb4f03f44ecc2028bd5
However, whatever I try to reset the request stream from the middleware, it is not working.
Replacing IOwinRequest.Body with a Memorystream / copying it, resetting it:
var bufferedReqStream = new MemoryStream();
await Request.Body.CopyToAsync(bufferedReqStream);
Request.Body = bufferedReqStream;
// other code
bufferedReqStream.Position = 0;
It looks like WebApi is not taking part in the whole owin process or at least it's not handling the body stream well.
My current solution is to stay away from 'GlobalConfiguration.Configure()' and use app.UseWebApi(..) with my own HttpConfiguration. This makes sure WebApi is correctly integrated in the whole owin pipeline.
But this creates a load of other problems because other components (like WebAPi help pages / NInject webapi integration / other 3rd party components) all depend on 'GlobalConfiguration.Configuration'.
Am I doing something wrong? or is WebApi really hijacking the http body stream, failing whenever some other owin middleware tries to access it.
Comments: Can you try the following workaround when using 5.1.1 and see if this fixes your issue: HttpContextBase contextBase = (HttpContextBase)context.Environment["System.Web.HttpContextBase"]; Stream buffreredRequestStream = contextBase.Request.InputStream; // other code buffreredRequestStream.Position = 0; Request.Body = buffreredRequestStream;