1
即使我正在创建WebClient的新实例,并遵循标准过程以确保WebClient已被GC删除,这甚至都不应该考虑:webclient从我之前检索到的服务器检索内容,并且只重新启动应用程序它是否会允许从我的服务器获取新内容。内容是一个简单的文本文件字符串,没有花哨的缓存,因为它在WinRT上工作得很好。WebClient和任务不检索新内容,但新内容可用?
这是一个谜,因为我试图做一个聊天框;我需要刷新以获取新内容,但WebClient首次返回它检索到的内容。
我的代码:
public async Task<string> RetrieveDocURL(Uri uri)
{
return await DownloadStringTask(new WebClient(), uri);
}
/*
public async Task<byte[]> RetrieveImageURL(string url)
{
return await _webClient.GetByteArrayAsync(url);
}
* */
private Task<string> DownloadStringTask(WebClient client, Uri uri)
{
var tcs = new TaskCompletionSource<string>();
client.DownloadStringCompleted += (o, e) =>
{
if (e.Error != null)
tcs.SetException(e.Error);
else
tcs.SetResult(e.Result);
};
client.DownloadStringAsync(uri);
return tcs.Task;
}
WebClient的缓存策略非常具有侵略性。如果每次查询相同的URL,则应该考虑在最后添加一个随机参数。类似于“”http://www.yourserver.com/yourService/?nocache=“+ DateTime.UtcNow.Ticks' – 2013-03-26 08:08:00
谢谢!这是解决方案!将此添加到答案中。 – 2013-03-26 10:52:04