Open thenik opened 1 year ago
That code sample is buggy and does not correctly follow the APM pattern. BeginXx methods may complete the operation synchronously, which is why the IAsyncResult.CompletedSynchronously property exists. The callback needs to check that property, and if it's true, exit immediately and instead allow the BeginXx call site to perform the continuation. Otherwise, a string of synchronously completing operations may stack dive. The docs should be fixed, but this isn't a bug in .NET Core nor is it configurable. .NET Core is simply faster and much more likely for operations to complete synchronously.
Thanks, got it. It is clear how it works now =) Funny that this shoud work from .NET 4.5 but on practice I do not see that even at .NET 4.8 . Only .net core sends CompletedSynchronously == true .
I found your comment here https://github.com/dotnet/runtime/issues/29024 also and it has the same story.
If anyone works in Microsoft or anyone knows anybody works in Microsoft ask Microsoft to adjust this samples =) because it is wrong now.
About this case the some relevant answer it to look at https://learn.microsoft.com/en-us/dotnet/api/system.iasyncresult.completedsynchronously?view=net-7.0
when it is possible to find
Notes to Callers Use this property to determine if the asynchronous operation completed synchronously. For example, this property can return true for an asynchronous I/O operation if the I/O request was small.
Hope that somebody will find that via google also. Thanks for your help again! Best regards. Nik
@thenik
If anyone works in Microsoft or anyone knows anybody works in Microsoft ask Microsoft to adjust this samples =) because it is wrong now.
Most things are open now in .Net, so we don't have to rely on Microsoft employees. Namely:
@thenik I wonder what is the reason you are using HttpWebRequest
and not HttpClient
?
@antonfirsov it is an old legacy project ( https://mydataprovider.com web scraper ). Started since Microsoft .NET Framework 2.0 . I migrate it to .net 6. So it is hard to use HttpClient. Additionally , in a real life I saw many times when WebClient, HttpClient, HttpWebRequest do not work as expected, all these classes can hang-up under the high load. So we if you want a reliable software (webscraper) you have to managing all http requests itself.
Hello ,
If you run this code sample https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.begingetresponse?view=net-7.0
on .net framework 462 or 472 it calls BeginRead on a ThreadPool and it is standart behavious that I expect. but on .net core 2.2, 3 and above it calls BeginRead on a stack and it is wrong behaviour because if you need to load a big file it will rise an exception StackOverflow.
So question : how to run BeginRead for Stream from GetResponseStream on a ThreadPool on .NET Core 6/7?
Here is my small modification of original code from the sample that calls stackoverflow at .net core 6 or .net core 7