0

I have the following code inside my asp.net mvc action method. the code will loop through a list of servers and for each server it will make a call to a third party system using WebClient() call.

                        //loop started
   try
      {


        using (WebClient wc = new WebClient()) // call the Third Party API to get the account id 
        {

          string url = currentURL + "resources/" + ResourceID + "/accounts?AUTHTOKEN=" + pmtoken;
          var json = await wc.DownloadStringTaskAsync(url);

         }

      }
   catch (Exception)
      {
       c.ScanResult = "Call to Third Party system Failed ";
       scan.Description = scan.Description + "<span style='color:red'>" + c.ScanResult + "</span><br/>";

       repository.Save2();
       continue;
      }
                      //end of loop

currently i am facing a problem if that when the loop start it will be working well,, and the WebClient() call will work normally... but after successive WebClient() requests the method will hang and seems the WebClient() call will hang for some reasons (maybe a problem in the third party system), and the whole method execution will stop (never ends), so can any one adivce how i can force the WebClient() to continue execution if it does not receive any response within certain amount of time (for example 5 minutes)? as currently no exception will be raised and the loop will never ends ..

Thanks

John John
  • 1
  • 72
  • 238
  • 501

1 Answers1

1

You can set the Timeout property on a WebRequest, but not a WebClient

More info here
https://stackoverflow.com/a/6994391/5367708

  private class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri uri)
    {
        WebRequest w = base.GetWebRequest(uri);
        w.Timeout = 5 * 60 * 1000;
        return w;
    }
}

So you can override the WebClient class and then use the MyWebClient instead.

Community
  • 1
  • 1
Stewart Parry
  • 128
  • 3
  • 8