question

VuyiswaMaseko-8882 avatar image
0 Votes"
VuyiswaMaseko-8882 asked VuyiswaMaseko-8882 edited

Java.Net.SocketException: Software caused connection abort

Good Day All

i have the following code and it randomly gives an error on Android 11

Java.Net.SocketException: Software caused connection abort

, not all the time , App Centre Reported it twice

 public async Task<List<USERS>> Get_All_Customers( )
           {
            
               List<USERS> results = null;
               try
               {
                   USERS m_model = new USERS
                   {
                       USER_ID = 1,
                       CURRENT_DATETIME = '2021/07/01'
                   };
                   var httpClient = new HttpClient();
            
                   httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                             
                             
                   var Json = JsonConvert.SerializeObject(m_model, new JsonSerializerSettings
                   {
                       ContractResolver = new DefaultContractResolver
                       {
                           IgnoreSerializableAttribute = false
                       }
                   });
            
            
                   HttpContent httpContent = new StringContent(Json, Encoding.UTF8, "application/json");
                   httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
            
                   var response = await Task.Run(() => httpClient.PostAsync(string.Format("https://www.myserver.com/api/MainCustomers_OnlineDates/Get_All_Customers"), httpContent));
            
                   if (response.StatusCode == System.Net.HttpStatusCode.OK)
                   {
                       var content = await response.Content.ReadAsStringAsync();
                       results = JsonConvert.DeserializeObject<List<USERS>>(content);
                   }
               }
               catch (Exception ex)
               {
                   await Task.Run(() => Crashes.TrackError(ex));
               }
            
               return results;
           }

Thanks

dotnet-xamarinvs-generalvs-debuggingdotnet-aspnet-webapi
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for posting the question here.

Since you have posted the question on azure-webapps tag, are you leveraging Azure App Service WebApp?

To receive insights from the targetted audience/SMEs on App center questions/issue, you may just post your question here:
https://github.com/microsoft/appcenter/issues and Help and feedback

I have added additional tags for other communities visibility.

0 Votes 0 ·
VuyiswaMaseko-8882 avatar image
0 Votes"
VuyiswaMaseko-8882 answered AgaveJoe edited

Thanks for the reply. This might have been reported via AppCentre i presume there is no issue there , the issue might be related Xamarin , Android and .net api

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The code wraps an asynchronous method, PostAsync, in Task.Run(). Task.Run starts a new thread. The thread executes an async method that is never awaited - send and forget. Remove the Task.Run and await the PostAsync.

 var response = await httpClient.PostAsync(string.Format("https://www.myserver.com/api/MainCustomers_OnlineDates/Get_All_Customers"), httpContent);

Also, your implementation of HttpClient can cause port exhaustion. Use a factory pattern to populate the HttpClient. There are other issues with the code like CURRENT_DATETIME = '2021/07/01' which I assume is a typo.

Anyway, fix the async/await bugs and the code should start working much better.


0 Votes 0 ·
VuyiswaMaseko-8882 avatar image
0 Votes"
VuyiswaMaseko-8882 answered VuyiswaMaseko-8882 edited

Thank you , i have made changes to the code .

1) Created a Static variable for the httpClient that can be used by other functions like this

 static readonly HttpClient httpClient = new HttpClient();

2)
and have changed the code by removing the recreation of the variable and also removed Task.Run and my function looks like this


 public async Task<List<USERS>> Get_All_Customers( )
            {
                
                List<USERS> results = null;
                try
                {
                    USERS m_model = new USERS
                    {
                        USER_ID = 1,
                        CURRENT_DATETIME = '2021/07/01'
                    };
                    var httpClient = new HttpClient();
                
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                                 
                                 
                    var Json = JsonConvert.SerializeObject(m_model, new JsonSerializerSettings
                    {
                        ContractResolver = new DefaultContractResolver
                        {
                            IgnoreSerializableAttribute = false
                        }
                    });
                
                
                    HttpContent httpContent = new StringContent(Json, Encoding.UTF8, "application/json");
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
                
 var response =  await httpClient.PostAsync(string.Format("https://www.myserver.com/api/MainCustomers_OnlineDates/Get_All_Customers"), httpContent);
    
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var content = await response.Content.ReadAsStringAsync();
                        results = JsonConvert.DeserializeObject<List<USERS>>(content);
                    }
                }
                catch (Exception ex)
                {
                    await Task.Run(() => Crashes.TrackError(ex));
                }
                
                return results;
            }

you can ignore the date part , just for an example. i will wait and see there is no reports of the same issues



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.