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
.Result?Resultis a classic example of a potential deadlock.