How to set timeout for RestClient in windows phone 8?
Asked Answered
S

1

6

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; }
}
Salvage answered 4/2, 2016 at 10:4 Comment(2)
Do you understand CONN_TIMEOUT=600000 means how much time its ? You got StatusCode=NotFound means 404 http server errorGannie
I didn't add CONN_TIMEOUT =600000 which is 10 minutes, at first, I started with 20 seconds, 30 seconds etc and as the server is performing some operations it takes a time to send a response. And as it takes more time than 100 seconds which is default time out, I am getting NotFound error, else for other web service it works perfectly.Salvage
O
0

Your code is probably using default HttpWebRequest.Timeout

Please try passing it in RestRequest explicitly like this -

request.AddParameter("Timeout", options.Timeout.Value);

You may try a better version like this -

var client = new RestClient(baseUrl)
{
    Timeout = someTimeoutInMilliseconds
};

var request = new RestRequest(url)
{
    Method = Method.GET,
    Timeout = someTimeoutInMilliseconds
};
Obvert answered 25/2, 2016 at 9:49 Comment(1)
Option is nothing but a variable that holds timeout. And that post talks about another/unrelated issue though code style copied over is much managed. It should be worth trying out these.Obvert

© 2022 - 2024 — McMap. All rights reserved.