Our mobile app heavily depends on continuous syncing between app and server. For this we run multiple api's in the background(parallely) to fetch the data (like Account, Acc related items etc).
Every 5 mins sync process starts and it instantiates 12 api's. I have a backgroundtask(where I', handling httpclient methods) where all the 12 api's calls this task to fetch it's respective data.
public BackgroundTask(string authToken)
{
client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
}
api call:
private async TTask PostReturnMultipleAsync(string api, object postContent, CancellationToken cancellationToken)
{
try
{
var serializedContent = JsonConvert.SerializeObject(postContent);
HttpContent httpContent = new StringContent(serializedContent, Encoding.UTF8, "application/json");
response = await client.PostAsync(Constants.BaseUrl + "/" + api, httpContent, cancellationToken);
}catch(Exception ex){}
}
The issue is sometimes I'm getting strange errors in some sync process related to network. Let's say sync process starts at 12:00 pm I will be getting errors in some api's and another sync process starts at 12:10 pm, then it works fine.
The errors details are as follows:
[6/23/2020 8:16:26 AM] The network connection was lost.
at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x1035be040 + 0x009a3> in <75e8f2cafde249969958834208668188#2628be6571b6c601843a6a945b784ff0>:0 at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) <0x1034e1cd0 + 0x005f4> in <692b89da4aac4c12a3ac3af90678ac25#2628be6571b6c601843a6a945b784ff0>:0 at TCRMobile.Tasks.BackgroundTask1[T].PostAsync (System.String api, System.Object postContent, System.Threading.CancellationToken cancellationToken) <0x103971430 + 0x00377> in <570cf8f88c3948a1b77300eead58c259#2628be6571b6c601843a6a945b784ff0>:0
[6/23/2020 8:16:26 AM] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=53, NSUnderlyingError=0x280158510 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=53, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <1695E60D-6530-4BD8-A2B2-08E8E8C3C317>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <1695E60D-6530-4BD8-A2B2-08E8E8C3C317>.<1>"
), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=https://apps.tcrsoftware.com/tcrservices/api/Tickets/GetTickets
[6/22/2020 1:43:13 PM] A task was canceled.
at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x101d92040 + 0x009a3> in <75e8f2cafde249969958834208668188#2628be6571b6c601843a6a945b784ff0>:0 at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean
I tried setting timeout to 50 seconds, that doesn't helps too. Any better to do this?