4

I develop au Universal App using MVVM-Light.

On a page, there is a list of comments coming from a WebService. If the current user is the author of a comment, I show a FlyoutMenu allowing him to "Edit" or "Delete" its comment. There is also a AppBarButton for adding a new comment:

enter image description here

My problem is that the comments are never refreshed after the first load of this page...

I use a "LoadComments()" method in the ViewModel that allows me to get the comments when I arrive on the page, but also after editing, deleted or added an item:

private async void LoadComments()
{
    List<Comment> commentsWS = await WebServiceGetCommentList();
    if (commentsWS != null)
        Comments = new ObservableCollection<Commentaire>(commentsWS);
}

This method so calls another method "WebServiceGetCommentList()" that prepares the call to the WebService, in the same ViewModel:

private async Task<List<Comment>> WebServiceGetCommentList()
{
    // Parameters
    List<KeyValuePair<string, string>> parametres = new List<KeyValuePair<string, string>>();
    parametres.Add(new KeyValuePair<string, string>("option", _currentUserAccount.option));
    parametres.Add(new KeyValuePair<string, string>("id_article", Article.id_article.ToString()));

    // Call WebService and deserialize
    Exception custEx = null;
    try
    {
        List<Comment> comments = await WebServices.GetCommentList(_currentUserAccount.url, parametres, "");
        return comments;
    }
    // Exceptions 
    catch (Exception e)
    {
        ...

    }
    return null;
}

I then go in the "GetComments()" method on the "WebServices" class:

public static async Task<List<Comment>> GetCommentList(String url, List<KeyValuePair<String, String>> parametres, String protocol)
{
    // Call WebService and deserialize
    var response = await JSONParser.getJSONFromUrl(url, parametres, "");
    List<Comment> comments = new List<Comment>();
    WsResponse wsResponse = ManageWsReponse(response, Constants.WebServiceTask.GetCommentList.Value);
    try
    {
        WsResponseResult wsResponseResult = JsonConvert.DeserializeObject<WsResponseResult>(wsResponse.data.ToString());
        comments = JsonConvert.DeserializeObject<List<Comment>>(wsResponseResult.result.ToString());
        return comments;
    }
    catch (Exception e)
    {
        throw new DeserializeException("Deserialize exception", e, DateTime.Now, "Comment");
    }
}

This method calls the "getJSONFromUrl()" method in the "JSONParser" class that launches the "client.GetAsync()":

public static async Task<string> getJSONFromUrl(String url, List<KeyValuePair<String, String>> parameters, String protocol)
{
    var client = new HttpClient();

    // Preparing URI
    string sParameters = null;
    int i = 1;
    foreach (var param in parameters)
    {
        sParameters += param.Key + "=" + param.Value;
        sParameters += i != parameters.Count ? "&" : string.Empty;
        i++;
    }
    var uri = new Uri(url + "?" + sParameters);

    // Calls the WebService
    var response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);

    // Code and results
    var statusCode = response.StatusCode;
    // EnsureSuccessStatusCode throws exception if not HTTP 200
    response.EnsureSuccessStatusCode();
    // responseText
    var responseText = await response.Content.ReadAsStringAsync();
    return responseText;
}

I can add, delete or edit a comment with success, but when I'm back to this method "LoadComments()", the changes are not taken into account, and I get the same list than at the first call...

I also placed breakpoints in the "GetJSONFromURL()" method and I don't see the added, deleted or edited comments in the response var.

In the same time, if I copy the URI in a brower, for calling the same WebService with the same parameters, the changes are taken into account.

=> I think so there is a caching on client.GetAsync(), but I don't see how to desactive it, or force it to refresh datas...

I tried this solution httpclient-caching that doesn't work for me.

I think so that there is cache managing cause when I

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Gold.strike
  • 1,269
  • 13
  • 47

1 Answers1

2

That's the platform caching. I haven't had any success with the Cache-Control headers, the most reliable way to address the issue is to make sure the request is different.

Add an additional parameter - a timestamp. As the request is different, the platform cannot used the cached response.

parametres.Add(new KeyValuePair<string, string>("mytmstmp", DateTime.Now.Ticks);

Or: Use an additional header that allows for a date. I've used:

"If-Modified-Since", DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)
lisp
  • 4,138
  • 2
  • 26
  • 43
  • Thanks for your return. I finally did it by using the "DefaultRequestHeaders" parameter "IfModifiedSince": client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow; – Gold.strike Oct 02 '15 at 16:00