Reputation: 1097
I'm working on windows app 8.1 Notification c# I had written one async method. I had also created scheduler for async method which is calling this method on every 5sec.
My problem is I always getting old records even if my webservice returns new records. If I close my application and start again then i am able to see the latest records.
My Webservice method returns json data.
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)
{
}
}
Upvotes: 1
Views: 77
Reputation: 1097
If anybody on the same boat just add below line:
HttpClient http = new System.Net.Http.HttpClient();
http.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
Upvotes: 1
Reputation: 36423
Firstly if you are in ASP doing a .Result is generally a bad idea because you are at risk of getting thread deadlocks: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
So I'd change this line too:
var webresponse = await response.Content.ReadAsStringAsync();
If it is an HTTP GET it could be that the results are being cached at a proxy or by your browser. To prevent this you can set the HTTP Headers to prevent caching: http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/
Upvotes: 1