0

I am moving my class library helper for HttpWebRequest (standard .net class) from .NET 4.6.2 to .NET core 3.1.

And I discovered the following issue with .NET core 3.1: When I call responseStream.BeginRead it calls via the Stack! So it is the reason of issue: when stack ends application fails due to stack overflow error.

.NET 4.6.2 behavior is different - it calls BeginRead on Thread Pool. And it works right for years without any issue.

I cannot find the reason why .NET core 3.1 has different behavior for BeginRead.

If you know how to fix HttpWebRequest BeginRead or any ideas share with me, please.

zhulien
  • 5,145
  • 3
  • 22
  • 36
TheNIK
  • 67
  • 1
  • 8
  • You may choose to use `System.Net.Http.HttpClient` in **.NET core** project. Related topics [When to use WebClient vs. HttpClient vs. HttpWebRequest](https://stackoverflow.com/questions/27793761/httpclient-vs-httpwebrequest-for-better-performance-security-and-less-connectio) and [HttpClient vs HttpWebRequest for better performance, security and less connections](https://www.infoworld.com/article/3198673/when-to-use-webclient-vs-httpclient-vs-httpwebrequest.html) – Thiam Hooi Jun 29 '21 at 11:17
  • Yes, I can, thanks! But my codebase is too big to change HttpWebRequest to HttpClient, started at 2009 – TheNIK Jun 29 '21 at 11:25
  • That's huge project. Perhaps building a wrapper on top of HttpClient to mitigate HttpWebRequest. – Thiam Hooi Jun 29 '21 at 13:36
  • yes, I think about that also, thanks – TheNIK Jun 30 '21 at 15:08

1 Answers1

0

It is standart behaviour for .net core and your code has to be ready for that.

You have to analyse in your code ReadIAsyncResult.CompletedSynchronously == true/false and decide what to do this that if CompletedSynchronously == true your have to return from the function.

Check docs

https://learn.microsoft.com/en-us/dotnet/api/system.iasyncresult.completedsynchronously?view=net-7.0

Notes to Implementers Most implementers of the IAsyncResult interface will not use this property and should return false. Beginning with the .NET Framework 4.5, a task that is created with the FromAsync(IAsyncResult, Action, TaskCreationOptions) method will not complete if this property is not implemented correctly. See Application Compatibility in 4.5.

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.

Find more details here https://github.com/microsoft/referencesource/issues/177

TheNIK
  • 67
  • 1
  • 8