1

I am using RestSharp (a REST client for .NET) in my Windows Phone 8 app, but I think my question also applies using HttpWebRequest or any other ways of running web requests.

I am trying to find a way to automatically resubmit the requests when app is resumed from dormant state. This is only from dormant and not from the tombstone state.

The idea I had was to create a wrapper object which subscribes to the Deactivated event before starting the request and rerunning the request in case it received the event. I assume that since the deactivated event was received, the request failed.

public class RestClientEx
{
    bool wasDeactivated = false;

    public async Task<T> ExecuteTaskAsync<T>(RestClient client, RestRequest request) where T : new()
    {
        var phoneApplicationService = App.Current.ApplicationLifetimeObjects.OfType<PhoneApplicationService>().First();
        phoneApplicationService.Deactivated += phoneApplicationService_Deactivated;
        var t = await client.ExecuteTaskAsync<T>(request);
        if (this.wasDeactivated)
        {
            // resubmit request
            this.wasDeactivated = false;
            t = await this.ExecuteTaskAsync<T>(client, request);
        }
        return t;
    }

    void phoneApplicationService_Deactivated(object sender, DeactivatedEventArgs e)
    {
        (sender as PhoneApplicationService).Deactivated -= phoneApplicationService_Deactivated;
        this.wasDeactivated = true;
    }
}

My question is, is there another way to achieve this?

Is it OK what I am doing?

4

0 回答 0