Description
Description
Some third-party code we're depending on does this:
var request = WebRequest.CreateHttp(url);
request.Method = "POST";
// Problem: Calling request.GetRequestStream() twice returns same stream object in .NET Framework,
// but different objects in .NET Core 3.1.
using (var writer = new StreamWriter(request.GetRequestStream()))
writer.Write("Hello, world!");
request.GetRequestStream().Close();
request.GetResponse();
Of course, there are things to improve here; Unfortunately, this code is not under our control, so we have no way to change it.
Anyway, the code works fine on .NET Framework 4.7.2 as the GetRequestStream
method returns the same System.Net.ConnectStream
object on both calls. In contrast, it fails on .NET Core 3.1.400, because the method returns a new instance on every call, which means that the above code just sends empty POST requests.
Expectation: GetRequestStream
should return the same object on every invocation.
Configuration
- .NET Core 3.1.400
- Windows 10.0.19041.388
- x64
Regression?
Yes. As outlined above, this works fine on .NET Framework 4.7.2, but does not work on .NET Core 3.1.
Other information
Here's the relevant code section, which shows that a new RequestStream
is created on every invocation:
runtime/src/libraries/System.Net.Requests/src/System/Net/HttpWebRequest.cs
Lines 1029 to 1056 in 6072e4d