WinHttpRequestStream.Flush Appends "0\r\n\r\n" in Chunked Mode
WinHttpRequestStream.Flush writes "0\r\n\r\n" to the stream every time the method is called in when _chunkedMode is true. This behavior seems wrong. The _endChunk should only be added to the end.
Because of this behavior WCF intermittently gets error saying 'The server returned an invalid or unrecognized response' (see dotnet/wcf#287.)
cc: @CIPop @SidharthNabar
@shmao Native WinHTTP doesn't handle chunked uploads on its own. So, we needed to implement the chunked encoding ourselves. The WinHttpRequestStream.Flush() method was meant to be called only once internally. I'll need to look at the repro to understand how it is being called multiple times. The stream itself is internal but perhaps it is being exposed thru the SerializeToStreamAsync() protected method and that custom HttpContent class is calling it multiple times?
@davidsh Yes, you were right. The stream can be accessed by inherited 'HttpContent' through 'SerializeToStreamAsync', and the 'Flush()' method can possibly be called at many places. You can have a look at the call stack of this test, which failed because of the issue.
Will be fixed as of PR dotnet/corefx#2907