I am calling a Webservice using RestSharp API.But I am facing a problem that when the server takes more time than RestClient's default timeout (i.e. 100 seconds) in that case I am always getting StatusCode=NotFound.I also tried to hard code both Timeout and ReadWriteTimeout parameters value but it did not help. Can someone suggest what can be done here?
My Code is as below
public async Task<WebDownloadResult> SyncMobileData(string encryptedstring)
{
WebDownloadResult response = new WebDownloadResult();
var client = new RestClient(BaseUrl + Constants.WEB_SERVICE_NAME);
client.Timeout = CONN_TIMEOUT;
client.ReadWriteTimeout = CONN_TIMEOUT;
var request = new RestRequest(Method.POST);
byte[] encryptedbytes=System.Text.Encoding.UTF8.GetBytes(encryptedstring);
request.AddParameter("", encryptedbytes, ParameterType.RequestBody);
try
{
response = await client.GetResponseBytesAsync(request);
}
catch (Exception outer)
{
response.ErrorOccured = true;
response.ErrorMessage = outer.Message;
}
return response;
}
where CONN_TIMEOUT=600000 and my response handling class is below:
public static class RestClientExtensions
{
private static Task<T> GetResponseFromServer<T>(this RestClient client, IRestRequest request, Func<WebDownloadResult, T> selector)
{
var tcs = new TaskCompletionSource<T>();
WebDownloadResult webResponse = new WebDownloadResult();
var loginResponse = client.ExecuteAsync(request, r =>
{
if (r.ErrorException == null && r.StatusCode == HttpStatusCode.OK)
{
webResponse.StatusCode = r.StatusCode;
webResponse.StatusCodeNumber = (int)r.StatusCode;
webResponse.Result = r.Content;
webResponse.ErrorOccured = r.StatusCode == HttpStatusCode.OK ? false : true;
tcs.SetResult(selector(webResponse));
}
else
{
tcs.SetException(new Exception(r.StatusCode.ToString()));
}
});
return tcs.Task;
}
private static Task<T> GetResponseFromServerForMW<T>(this RestClient client, IRestRequest request, Func<WebDownloadResult, T> selector)
{
var tcs = new TaskCompletionSource<T>();
WebDownloadResult webResponse = new WebDownloadResult();
var loginResponse = client.ExecuteAsync(request, r =>
{
if (r.ErrorException == null && r.StatusCode == HttpStatusCode.OK)
{
webResponse.StatusCode = r.StatusCode;
if (r.Headers.FirstOrDefault(header => header.Name.Equals("P_RET_SYNC_ID")).Value != null)
{
webResponse.AdditionalParameter = r.Headers.FirstOrDefault(header => header.Name.Equals("P_RET_SYNC_ID")).Value;
}
webResponse.StatusCodeNumber = (int)r.StatusCode;
webResponse.Result = r.RawBytes;
webResponse.ErrorOccured = r.StatusCode == HttpStatusCode.OK ? false : true;
tcs.SetResult(selector(webResponse));
}
else
{
tcs.SetException(new Exception(r.StatusCode.ToString()));
}
});
return tcs.Task;
}
private static Task<T> GetRawResponseFromServer<T>(this RestClient client, IRestRequest request, Func<WebDownloadResult, T> selector)
{
var tcs = new TaskCompletionSource<T>();
WebDownloadResult webResponse = new WebDownloadResult();
var loginResponse = client.ExecuteAsync(request, r =>
{
if (r.ErrorException == null && r.StatusCode == HttpStatusCode.OK)
{
webResponse.StatusCode = r.StatusCode;
webResponse.StatusCodeNumber = (int)r.StatusCode;
webResponse.Result = r.RawBytes;
webResponse.ErrorOccured = r.StatusCode == HttpStatusCode.OK ? false : true;
tcs.SetResult(selector(webResponse));
}
else
{
tcs.SetException(new Exception(r.StatusDescription));
}
});
return tcs.Task;
}
public static Task<WebDownloadResult> GetResponseAsync(this RestClient client, IRestRequest request)
{
return client.GetResponseFromServer(request, r => r);
}
public static Task<WebDownloadResult> GetResponseAsyncForMW(this RestClient client, IRestRequest request)
{
return client.GetResponseFromServerForMW(request, r => r);
}
public static Task<WebDownloadResult> GetResponseBytesAsync(this RestClient client, IRestRequest request)
{
return client.GetRawResponseFromServer(request, r => r);
}
}
and WebDownloadResult is below:
public class WebDownloadResult
{
public HttpStatusCode StatusCode { get; set; }
public string ErrorMessage { get; set; }
public int StatusCodeNumber { get; set; }
public bool ErrorOccured { get; set; }
public object Result { get; set; }
public object AdditionalParameter { get; set; }
}