总是从windows应用程序8.1 c#中的webservice获得相同的结果?
我正在开发windows应用程序8.1通知c#我编写了一个异步方法。我还为异步方法创建了调度器,它每5秒调用一次这个方法 我的问题是,即使我的Web服务返回新记录,我也总是得到旧记录。如果我关闭我的应用程序并重新启动,那么我就能看到最新的记录 我的Webservice方法返回json数据总是从windows应用程序8.1 c#中的webservice获得相同的结果?,c#,web-services,windows-phone-8.1,async-await,C#,Web Services,Windows Phone 8.1,Async Await,我正在开发windows应用程序8.1通知c#我编写了一个异步方法。我还为异步方法创建了调度器,它每5秒调用一次这个方法 我的问题是,即使我的Web服务返回新记录,我也总是得到旧记录。如果我关闭我的应用程序并重新启动,那么我就能看到最新的记录 我的Webservice方法返回json数据 private int timesTicked = 1; private int timesToTick = 10; public NotificationPage() { InitializeCom
private int timesTicked = 1;
private int timesToTick = 10;
public NotificationPage()
{
InitializeComponent();
// this._channelUri = LocalSettingsLoad(ApplicationData.Current.LocalSettings, ChannelUriKey, ChannelUriDefault);
Task<string> getURi = UpdateChannelUri();
DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1000) };
timer.Tick += (s, e) =>
{
getNotificationFrom_Api();
};
timer.Start();
}
private async void getNotificationFrom_Api()
{
try
{
string uri = "http://abc/service1.svc/GetNotificationList"; //testing purpose
HttpClient http = new System.Net.Http.HttpClient();
HttpResponseMessage response = await http.GetAsync(uri);
var webresponse = response.Content.ReadAsStringAsync().Result;
var emp = JsonConvert.DeserializeObject<ClNotification>(webresponse);
ListBox1.ItemsSource = emp.GetNotificationlistResult.ToList();
}
catch (Exception rt)
{
}
}
private int timesTicked=1;
专用int timesToTick=10;
公共通知页()
{
初始化组件();
//这是._channelUri=LocalSettingsLoad(ApplicationData.Current.LocalSettings,ChannelUriKey,ChannelUriDefault);
Task getURi=UpdateChannelUri();
调度程序计时器=新调度程序{Interval=TimeSpan.From毫秒(1000)};
计时器.勾号+=(s,e)=>
{
getNotificationFrom_Api();
};
timer.Start();
}
私有异步void getNotificationFrom_Api()
{
尝试
{
字符串uri=”http://abc/service1.svc/GetNotificationList“;//测试目的
HttpClient http=new System.Net.http.HttpClient();
HttpResponseMessageResponse=等待http.GetAsync(uri);
var webresponse=response.Content.ReadAsStringAsync().Result;
var emp=JsonConvert.DeserializeObject(webresponse);
ListBox1.ItemsSource=emp.GetNotificationlistResult.ToList();
}
捕获(异常rt)
{
}
}
首先,如果您在ASP中执行。结果通常不是一个好主意,因为您面临线程死锁的风险:
所以我也要改变这一行:
var webresponse = await response.Content.ReadAsStringAsync();
如果是HTTP GET,则可能是结果正在代理或浏览器中缓存。要防止出现这种情况,可以设置HTTP头以防止缓存:
如果有人在同一条船上,只需添加以下行:
HttpClient http = new System.Net.Http.HttpClient();
http.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
我在上面做了尝试,仍然得到了相同的结果,但我应该如何清除代码中的缓存我需要在我的Web服务或c#方法中处理缓存