ReadEntityBody changed behavior

I have an HttpModule that acts like a file upload module, and after updating the .NET framework to 4.5 it works differently. With Framework 4.0, the ReadEntityBody method populated a 256k array, but after updating it will return only 16k. Does anyone else have this problem?

public void ProcessRequest(HttpContext context) { IServiceProvider provider = (IServiceProvider)context; HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService( typeof(HttpWorkerRequest)); byte[] data = new byte[256 * 1024]; int readData = worker.ReadEntityBody(data, data.Length); // ...... } 
+6
source share
2 answers

We ran into this, and we had to adapt. In fact, in production, we found that at least 16 KB can often be returned, probably because less time is available in this environment.

Personally, I look at this as error 4.5, because the behavior of ReadEntityBody is not documented to return less than requested, so this is a change from 4.0 to 4.5.

Stream.Read , on the other hand, explicitly documents this behavior:

An implementation may return fewer bytes than requested, even if the end of the stream has not been reached.

So, if you look at it from a different angle, ReadEntityBody has the same API as Stream.Read , and you should expect from it that it will have the same semantics. In this sense, 4.5 (webengine4.dll) simply changed the implementation by executing the same contract.

IMO, in the worst case this is a terrific change, in the best case it is a documentation error. Some do not consider this either one or the other. You can decide.

I did not feel motivated to file a mistake. If it worked from day one, I probably would have thought it logical. It's just a shame that it broke in the supposed frame update 100% ago. C'est la vie ..

+1
source

Finally, I found a solution to this problem. Created an extension for the HttpWorkerRequestExtension class, which will fill the buffer before returning my call.

 public static class HttpWorkerRequestExtension { public static int ReadEntityBodyEx(this HttpWorkerRequest request, byte[] buffer, int offset, int size) { int bytesRead = 0; int totalBytesRead = 0; int bytesToRead = size; while (bytesToRead > 0) { bytesRead = request.ReadEntityBody(buffer, offset + totalBytesRead, size - totalBytesRead); if (bytesRead == 0) { break; } bytesToRead -= bytesRead; totalBytesRead += bytesRead; } return totalBytesRead; } public static int ReadEntityBodyEx(this HttpWorkerRequest request, byte[] buffer, int size) { return request.ReadEntityBodyEx(buffer, 0, size); } } 
0
source

All Articles