Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

WebClient and caching RSS

19 replies

Last post Dec 29, 2010 03:29 PM by Viento85

(0)
  • bennedik

    bennedik

    Member

    7 Points

    11 Posts

    WebClient and caching

    Apr 18, 2008 12:52 PM | LINK

    I am using the WebClient to download a resource. The resource can change on the server, so I have added a refresh button to the Silverlight application. However, WebClient seems to download through the cache. I am not getting an updated resource.

    I have to click the refresh button of the browser to get the updated resource. However, this is not ideal, as this refreshes the whole page.

    How can I bypass caching when using WebClient?

    Martin Bennedik
  • Yi-Lun Luo - MSFT

    Yi-Lun Luo -...

    All-Star

    25149 Points

    2759 Posts

    Microsoft

    Re: WebClient and caching

    Apr 21, 2008 06:52 AM | LINK

    Hello, the trick is to add an unused random request parameter:

    Random random = new Random();

    string url = "http://...?Unused=" + random.Next().ToString();

    shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
  • bennedik

    bennedik

    Member

    7 Points

    11 Posts

    Re: WebClient and caching

    Apr 21, 2008 09:29 AM | LINK

    Thank you, I confirm this workaround. Any chance for a "clean" solution in the 2.0 final?

    Martin Bennedik
  • Ozzy1

    Ozzy1

    Member

    40 Points

    33 Posts

    Re: WebClient and caching

    Apr 29, 2008 04:36 AM | LINK

    WPF has a "clean" way to bypass the cache when loading images. I would REALLY like to see that feature make it into SL.

  • bennedik

    bennedik

    Member

    7 Points

    11 Posts

    Re: WebClient and caching

    Apr 29, 2008 11:28 AM | LINK

    I think the work-around is dirty and not acceptable for the final version of Silverlight 2. I have opened a bug for this on Connect, see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=340931. Please vote for it.

    Martin Bennedik
  • Ozzy1

    Ozzy1

    Member

    40 Points

    33 Posts

    Re: WebClient and caching

    Apr 29, 2008 07:18 PM | LINK

    I added my vote. Thank you!

  • daxsorbito

    daxsorbito

    Member

    12 Points

    6 Posts

    Re: WebClient and caching

    Jun 17, 2008 03:30 AM | LINK

    Is this already fix using Silverlight 2 beta 2? It seems I'm having the same problem.

  • bennedik

    bennedik

    Member

    7 Points

    11 Posts

    Re: WebClient and caching

    Jun 17, 2008 08:52 AM | LINK

    I can also still reproduce the issue in Silverlight 2 beta 2. I reported so at https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=340931. Please consider voting and validating the issue at Connect.

    Martin Bennedik
  • daxsorbito

    daxsorbito

    Member

    12 Points

    6 Posts

    Re: WebClient and caching

    Jun 17, 2008 08:59 AM | LINK

    I hope at least they could replicate this and provide a neat solution. It seems it's very easy to replicate this issue. I already voted on this.

  • moemeka

    moemeka

    Member

    149 Points

    70 Posts

    Re: WebClient and caching

    Jul 11, 2008 10:11 PM | LINK

    The following code snippet can be added to your common silverlight library or directly to the silverlight app to resolve this issue.

    WARNING!!

    This is a workaround that utilizes extension methods. Meaning it may appear as though it is part of the WebClient API to an individual who is not using visual studio to edit c# files!  To make is more evident, please consider using a different namespace than System.Net.

    namespace System.Net {

      public static class WebClientExtensions {

         public static void DownloadStringAsync(this WebClient client, Uri uri, object user_token, bool fresh_copy) {

                    if (!fresh_copy) {

                         client.DownloadStringAsync(uri, user_token);

                    } else {

                         client.DownloadStringAsync(new Uri(uri.AbsoluteUri + "?" + Guid.NewGuid().ToString()), user_token);

                   }

          }

      }

    }

    Please mark as answered if this post resolves your issue.

    http://moemeka.blogspot.com