2
votes
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri(url, UriKind.RelativeOrAbsolute));


void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{ 
  if (e.Error != null || e.Cancelled)
  {
    //randomly, this code gets hit and 
    //there is no inner exception - just says "an exception occurred during a webclient request"

The error described in code happens randomly and if the same webclient request is made after that it works. Or if a new resource request is made the old one which got an error now comes before the new request......

This is very strange, any gurus here have a clue why it might be?

EDIT: information on error

System.Net.WebException: An exception occurred during a WebClient request. ---> System.Exception ---> System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component. at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.BrowserHttpWebRequest.<>c_DisplayClass5.b_4(Object sendState) at System.Net.AsyncHelper.<>c_DisplayClass2.b_0(Object sendState) --- End of inner exception stack trace --- at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result) --- End of inner exception stack trace ---

The URL is a simple string having count of no more than 200.

3
What's the type of the exception? Have you tried looking at what's happening at the network level using Wireshark? - Jon Skeet
@VoodooChild: That's the message of the exception possibly - but you should be seeing an actual exception... if your logging only contains the message but not the exception type, it's worth fixing that. - Jon Skeet
@Jon Skeet: Hi, the error was being caused by a third party control in my app, which was blocking the UI thread (silverlight app); the async call to the webclient (which runs on a different thread) was somehow ? (not sure about this part) affected and that's why we were seeing the error in the webclient completed. If I make the same webclient async call using the Dispatcher class which will execute the code on the UI Thread, then this error seems to get resolved and webClient completed is executed without any errors. - VoodooChild
@VoodooChild: That's very odd. I thought the WebClient async call would try to use the dispatcher thread... it does on Windows Phone 7. Oh well - glad it's resolved. - Jon Skeet
@tomasK - I don't remember all the details, however it seems to be a threading issue. The UI thread which calls the webClient method might have been blocked and carries out the any requests in its queue on the next usage. - VoodooChild

3 Answers

2
votes

That code would be hit if cancelled or error being generated. I suspect you are hitting a networking issue, so I would fire up Fiddler and watch the HTTP requests and make sure you are getting them back correctly.

0
votes

I am going to hazard a guess that it's timing out. I am also going to further guess that it may be a DNS issue.

It's possible that the DNS is timing out. This would be consistent with you not seeing the request in Fiddler.

I suppose it's not impossible that the request would then be filled later when you try again, and this time the DNS works. Maybe. This point I'm a bit fuzzy on, but whatever.

If you use a URL with an IP address instead of a domain name, do you still have the same problem?

0
votes

Probably URL is very long. I've same issue on WP8 with 2900 symbols length URL