c# - client.GetAsync 不刷新我的数据

标签 c# windows-phone-8.1 win-universal-app dotnet-httpclient getasync

我使用MVVM-Light开发通用应用

在一个页面上,有一个来自 WebService 的评论列表。如果当前用户是评论的作者,我会显示一个 FlyoutMenu 允许他“编辑”或“删除”其评论。还有一个用于添加新评论的 AppBarButton:

enter image description here

我的问题是在第一次加载此页面后评论永远不会刷新...

我在 ViewModel 中使用了“LoadComments()”方法,它允许我在到达页面时获取评论,而且在编辑、删除或添加项目之后:

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

此方法调用另一个方法“WebServiceGetCommentList()”,在同一个 ViewModel 中准备对 WebService 的调用:

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;
}

然后我进入“WebServices”类的“GetComments()”方法:

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");
    }
}

此方法调用启动“client.GetAsync”的“JSONParser”类中的“getJSONFromUrl()”方法()":

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;
}

我可以成功地添加、删除或编辑评论,但是当我返回此方法“LoadComments()”时,所做的更改不会生效帐户,我得到了与第一次通话时相同的列表...

我还在“GetJSONFromURL()”方法中放置了断点,但我没有在响应变量中看到添加、删除或编辑的评论。

同时,如果我在浏览器中复制 URI,以使用相同的参数调用相同的 WebService,更改将被考虑在内。

=> 我认为 client.GetAsync() 上有一个缓存,但我不知道如何停用它或强制它刷新数据 ...

我试过这个解决方案 httpclient-caching这对我不起作用。

我认为当我有缓存管理的原因

最佳答案

那是平台缓存。我在 Cache-Control header 方面没有取得任何成功,解决该问题的最可靠方法是确保请求不同。

添加一个附加参数 - 时间戳。由于请求不同,平台无法使用缓存的响应。

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

:使用允许日期的附加标题。我用过:

"If-Modified-Since", DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)

关于c# - client.GetAsync 不刷新我的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32883964/

相关文章:

c# - 输入对话框消息框(Windows Phone 8.1 通用)

listview - Windows Store 应用程序在 ListView 之间拖放

c# - 在新的 Windows 10 CalendarView 上显示/设计额外信息

c# - 为什么在方法定义中使用 "Where"子句

c# - 无法将 foreach 更改为 LINQ 语法

camera - Windows Phone 8.1 MediaCapture CapturePhotoToStorageFileAsync 内存泄漏

c# - 使用 Windows 应用商店应用获取 CPU 使用率(等)

c# - 取消设置枚举标志

c# - 在哪里可以找到 Microsoft.Build.Utilities.v3.5

c++ - CM_Get_Device_Interface_List 返回 CR_INVALID_POINTER