WebClient REST service call works in console application but not in asp.net

I call a test REST web service that basically takes a string as input and returns it to the caller. I have the following code in a C # console application:

static async Task RunAsync()
{
    using (var client = new HttpClient())
    {
    string baseAddress = 
    "http://xxx.xxx.xxx.xxx/Services/OnyxCloudSyncService.svc/pingSync";
    client.BaseAddress = new Uri(baseAddress); 
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new
       MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = await 
               client.GetAsync("?sampleJson={SAMPLEJSON}");           
    if (response.IsSuccessStatusCode)
    {
        string txtBlock = await response.Content.ReadAsStringAsync(); 
        Console.WriteLine(txtBlock);
        Console.ReadKey();
    }
    }
}

This code works fine. But when I essentially copy the same code into the program code for the ASP.NET page, I end up waiting for the service to be called:

using (var SyncClient = new HttpClient())
  {
   string baseAddress = "http://xxx.xxx.xxx.xxx/Services/OnyxCloudSyncService.svc/pingSync";
   SyncClient.DefaultRequestHeaders.Accept.Clear();
   SyncClient.DefaultRequestHeaders.Accept.Add(new
           MediaTypeWithQualityHeaderValue("application/json"));
   HttpResponseMessage response = await 
          SyncClient.GetAsync("?sampleJson={SAMPLEJSON}");
   if (response.IsSuccessStatusCode)
   {
    string txtBlock = await response.Content.ReadAsStringAsync();
    Response.Write(txtBlock);
    Response.End();
   }
   else
   {
    Response.Write("Error Calling service");
    Response.End();
   }
  }

The error I get from this page is:

System.Net.Sockets.SocketException: A connection attempt failed because
the connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
xxx.xxx.xxx.xxx:80. 

Will there be any type of installation or option that I need to install on the WebClient so that it works on the ASP page, as is done in the Console application? I am at a loss as to why this will work in a console application and not on an ASP.NET web page.

+4
1

, , : http://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx

, .NET 4.5. , , Response:

async Task RunAsync()
{
    using (var client = new HttpClient())
    {
        string baseAddress =
        "http://74.120.219.166/Services/OnyxCloudSyncService.svc/pingSync";
        client.BaseAddress = new Uri(baseAddress);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new
           MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = await
                   client.GetAsync("?sampleJson={SAMPLEJSON}");
        if (response.IsSuccessStatusCode)
        {
            string txtBlock = await response.Content.ReadAsStringAsync();
            Response.Write(txtBlock);
            Response.End();
        }
        else
        {
            Response.Write("Error Calling service");
            Response.End();
        }
    }
}

: RegisterAsyncTask(new PageAsyncTask(RunAsync)); Async="true" Page .aspx. , , .

+1

Source: https://habr.com/ru/post/1584493/


All Articles