Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient in a WP7.1 app called only once

My problem is:

My WebClient uses a function in the Cloud (http://127.0.0.1:81/Service1.svc/Data).

But it is impossible to call several times (for example to make an update).

Here is my code:

private void button_Click(object sender, RoutedEventArgs e)
{
WebClient cnt = new WebClient();
cnt.DownloadStringCompleted += new DownloadStringCompletedEventHandler(cnt_DownloadStringCompleted);
cnt.DownloadStringAsync(new Uri("http://127.0.0.1:81/Service1.svc/Data"));
}

void cnt_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
int num = JsonHelper.FromJson<int>(e.Result);
textbox.Dispatcher.BeginInvoke(() => textbox.Text = Convert.ToString(num));
}

After each click, the application goes well in cnt_DownloadStringCompleted but the result (e.Result) never changes after an update in the server.

At the Azure service, I noticed through a break that the function (Data) is called only once (first time).

How can I do to call my service more than once?

like image 718
FBruynbroeck Avatar asked Dec 05 '25 05:12

FBruynbroeck


2 Answers

Check your server side to make sure that you aren't allowing caching. If you have the response context in your service, set:

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Alternatively, if you don't have the ability to do this, send in a changing nonsense parameter on the querystring, something like:

http://127.0.0.1:81/Service1.svc/Data?nonsense=190AF142-4341-47DC-9CF5-3BC3ACBD02EE

You can either generate and send in a new guid each time, or even use DateTime.Now.Ticks.ToString(). That being said, if you have control over the server side, eliminate caching, as this way is a serious hack.

like image 183
Robaticus Avatar answered Dec 07 '25 17:12

Robaticus


I believe web requests are often cached by the emulator (maybe the real phone too).

Here are a few blog posts that mention this, as well as some solutions:

  • http://www.benday.com/2011/10/06/disable-rest-webrequest-url-caching-on-wp7-mango/
  • http://www.nickharris.net/2010/10/windows-phone-7-httpwebrequest-returns-same-response-from-cache/
like image 33
mcollier Avatar answered Dec 07 '25 18:12

mcollier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!