As the title says, when downloading a file and reading the stream in a loop and writing to disk, sometimes the download gets stuck in a weak network environment (disk file size does not change for a long time), and the application does not appear abnormal. What is the reason? How to avoid and solve it?
By browsing the website, the known solution is to detect whether the size of the disk file changes within a period of time, and re-initiate the download request if the file size does not change. Is there any other solution?
Thanks to everyone who provided advice!
Mohammad HussainPosted Aug 7, 2023, 9:24 AM
Implement Timeout Mechanism: Set a timeout for the HttpClient request to prevent it from getting stuck indefinitely. You can use the
HttpClient.Timeoutproperty or use aCancellationTokenwith a specific timeout duration when making the request.Use HttpClient Retry Policies: Implement retry policies using libraries like Polly to automatically retry failed requests in case of network issues. This can improve the resilience of your application in weak network environments.
Check Network Connectivity: Before initiating the download, check for network connectivity to ensure that the device is connected to the internet. If the network connection is weak or unavailable, handle the situation gracefully.
Tahir AnsariPosted Aug 8, 2023, 4:38 AM
In a weak network environment, the download might get stuck due to network timeouts or interruptions. Another approach to handle this issue is to set a timeout for the HttpClient request itself, so it doesn't wait indefinitely. Additionally, you can implement retry logic with an increasing delay between retries to handle transient network issues.