1

The problem:

In a WebApi controller i am trying to make a http request to another website using HttpClient - But it allways fails, and says that the host doesnt respond, or that it failed to setup a connection.

How to reproduce:

In Visual Studio (Enterprise on this end) 2017, Create a new Project using the Azure Web Api Template (File -> new -> Project -> ASP.NET Web Application (.NET Framework) -> Ok -> Azure Web Api.

Make sure .NET Framework 4.7.1 is selected. (I have not tested with any other frameworks except .net core)

In the ValuesController, from the Get method, make a call to any URL and try to respond with the Result StatusCode.

Code::

public class ValuesController : ApiController
{
    // GET api/values
    [SwaggerOperation("GetAll")]
    public IEnumerable<string> Get()
    {
        using (var client = new HttpClient())
        {
            var result = client.GetAsync("https://www.google.com").Result;
            return new string[] { result.StatusCode.ToString(), "value2" };
        }
    }
}

Debugging:

  • I created a separate Console Application, and did the same thing there. [Works]
  • I created a separate .net core web api project, and did the same thing there. [Works]

I did a fair bit of searching around this error, but with no result on my end.

Any tips?

Exception Details:

System.AggregateException occurred
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=<Cannot evaluate the exception source>
  StackTrace:
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at WebApplication2.Controllers.ValuesController.Get() in C:\Users\<user>\documents\visual studio 2017\Projects\WebApplication2\WebApplication2\Controllers\ValuesController.cs:line 19
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

Inner Exception 1:
HttpRequestException: An error occurred while sending the request.

Inner Exception 2:
WebException: Unable to connect to the remote server

Inner Exception 3:
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 216.58.209.99:443
5
  • 1
    What is the exact and complete exception ? Why are you blocking the async call with .Result? Commented Nov 10, 2017 at 12:30
  • Just a result of me testing out different ways, and that edit making it into here. Making it async does not change the result. Exception attached to original post :) Commented Nov 10, 2017 at 12:34
  • There are problems with that site when I try to go to that IP address. It attempts some xhr calls in the background that fail. I can't explain why it works in your other project types. What if you hit a true, json only, service like SpaceX? api.spacexdata.com/v1/launches Commented Nov 10, 2017 at 12:42
  • The site, if your thinking about the IP that shows up in the exception, is www.google.no! The exception just shows the interpreted address. Commented Nov 10, 2017 at 14:07
  • 1
    You may want to read Don't Block on Async Code - your use of Result is a classic example of a potential deadlock. Commented Nov 10, 2017 at 14:51

1 Answer 1

4

Problem Solved - Solution:

Im behind a proxy, and it seems like either IIS Express or something within .net Framework, does not pick up the proxy settings that is defined in the system automatically like .net Core did, and the console application.

Adding the proxyaddress to web.config with the proxy address did the trick.

<system.net>
    <defaultProxy>
      <proxy usesystemdefault="True" proxyaddress="http://www-proxy.somedomain.com:80" bypassonlocal="False"/>
      <bypasslist>
        <add address="[a-z]+\.somedomain\.com$"/>
        <add address="[a-z]+\.somedomain\.net$"/>
      </bypasslist>
    </defaultProxy>
  </system.net>

Hope this helps anyone else running in to this issue!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.