2
votes

I have a Asp.Net Core (2.1) website using Asp.Net Boilerplate.

I'm trying to make a API call using HttpClient, which works locally, but throws an error when deployed to IIS.

I created a TypedClient

public class FormsHttpClient : IFormsHttpClient
{
    private const string url = "my_url:3001/";
    private readonly HttpClient _client;

    public FormsHttpClient(HttpClient httpClient)
    {
        httpClient.BaseAddress = new Uri(url);

        _client = httpClient;
    }
    ....
}

In my startup.cs file, I added:

services.AddHttpClient<IFormsHttpClient, FormsHttpClient>();

The error i get when i deploy:

ERROR 2018-12-12 18:30:29,509 [11 ] Mvc.ExceptionHandling.AbpExceptionFilter - 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 System.Net.Http.HttpRequestException: 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 ---> 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 at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) --- End of inner exception stack trace ---

The network call normally takes 1800ms to finish, it seems to timeout around 21000ms.

I came across the code AppContext.SetSwitch("System.Net.Http.useSocketsHttpHandler", false);, thought i wasn't sure where to put it, i tossed it in the constructor of my startup.cs class but it doesn't seem to have any effect.

Is there something else im missing?

E: The code that calls the api is in JS:

   app.forms.token = {
    generate: function () {
        Formio.logout();
        abp.services.app.forms.generateFormToken().done(function (e) {
            Formio.setToken(e);
        });
    },
    clear: function () {
        Formio.logout();
    }
}
1
Where are you making the request?! This is just registering service...Kos
If it works locally, then your code is fine. Are you sure you can actually reach that web service (my_url:3001) from the server you deployed this to?Gabriel Luci
@kos making the request in JS, the call returns 500 @gabriel, i can make the same api call in postman from the same device, so i can definitely access it. It seems the be the failing on any HttpClient's PostAsync or GetAsync, depending on which call i makelizzy91
That might be issue with that my_url:3001 service. In production you are requesting something that make it die. I would try to use some tool to capture the request like fiddler to investigate furder...Kos
@Kos thanks. I figured out a work around which is probably a good solution of its own. I had to change the IP to an internal IP (10.200.x.xx) for my server code and keep using the public IP in client side code. I was basically trying to access the same server on a different port. learning is fun =Dlizzy91

1 Answers

1
votes

Since I am communicating with the same machine but different port, I changed the public IP in my server code to an internal IP and my problem was resolved.

I've made it a rule of thumb to use internal IP in server code and public IP in client code. If this is bad practice, let me know.