Hello, I am creating a transmission application for a radio, for this I am using the XamarinMediaManager plugin to reproduce the transmission and additionally through HttpClient I get the song covers and I show them in the app, so far in theory everything is fine.
The problem that arises is that where I am I have huge but huge connectivity problems, so when the internet goes down, the application closes unexpectedly (crash).
To find out exactly the fault I have used the Microsoft App Center through Diagnostic I have obtained 2 causes.
The first is that when the internet goes down and at that moment the HttpClient is being used to obtain the cover, it will crash either by "System.Net.Sockets.SocketException: Network subsystem is down", "System.IO.IOException: Remote prematurely closed connection. "," System.Net.Http.HttpRequestException: Network is unreachable "or some Timeout. To try to avoid the Crash, I have tried with Try and Catch, with the Connectivity pluging and also with the Xamarin Essentials plugin, but they do not always detect when the internet goes down, finally I have tried with Polly to retry and try to stop the expecion
retryPolicy = Policy
.Handle ()
.Or ()
.Or ()
.OrResult (r => httpStatusCodesToRetry.Contains (r.StatusCode))
but nothing seems to work to prevent the app crash.
And second, similarly it happens to me with the XamarinMediaManager plugin, but the latter I think is more complex to avoid the app crash, since I think it should be handled inside the plugin, even so I also try to detect when there is no internet to place pause and avoid the error (this is notified in the github of plugin # 552) some of the errors that appear to me when the internet falls are:
1) MediaExtractor.ExtractMetadata (MediaManager.Media.IMediaItem mediaItem)
C: \ Users \ mhvdi \ Documents \ OpenSource \ XamarinMediaManager \ MediaManager \ Platforms \ Android \ Media \ MediaExtractor.cs, line 32
Java.Lang.RuntimeException: setDataSource failed: status = 0x80000000
2) MediaItemExtensions.GetImageBitmapFromUrl (System.String url)
C: \ Users \ mhvdi \ Documents \ OpenSource \ XamarinMediaManager \ MediaManager \ Platforms \ Android \ Media \ MediaItemExtensions.cs, line 202
System.Net.WebException: The operation has timed out.
3) MediaItemExtensions.GetImageBitmapFromUrl (System.String url)
C: \ Users \ mhvdi \ Documents \ OpenSource \ XamarinMediaManager \ MediaManager \ Platforms \ Android \ Media \ MediaItemExtensions.cs, line 201
System.Net.WebException: Error: NameResolutionFailure
4) MediaItemExtensions.GetImageBitmapFromUrl (System.String url)
C: \ Users \ mhvdi \ Documents \ OpenSource \ XamarinMediaManager \ MediaManager \ Platforms \ Android \ Media \ MediaItemExtensions.cs, line 204
System.Net.WebException: Error getting response stream (ReadDoneAsync2): ReceiveFailure
5) MediaItemExtensions.GetImageBitmapFromUrl (System.String url)
System.Net.WebException: The operation has timed out.
In summary, I would like you to please give me some idea or advice on how I can handle or prevent the application from closing when the internet does not suddenly drop. Perhaps there is some more robust pattern or mechanism to handle these types of exceptions.
Thank you so much.