1

I have C# class liblary .NET Framework 4.7.2 who run as plugin for cashbox system(iikofront) in new process.

In work flow program call HTTPS API in Internet. That http request code enter image description here

In plugin logs:

[INFO] 2020-10-20 13:40:23 Start SendAsync  
[INFO] 2020-10-20 13:40:45 End SendAsync  

In server load balancer:

2020-10-20 13:40:34 Start process request
2020-10-20 13:40:36 End process request

PROBLEM
HTTP Request come in server after 10-12 seconds and exist cases when delay 20-30 seconds and Request was abort by HttpClient Timeout. This behavior repeat for first http request, rest requests handled without delay.

QUESTION
What could be the reason for the delay?

INFORMATION
Internet connection fast check in browser
Behavior not repeat in browser if call GET request in browser adress field

HttpClient is created for every request by this code:

public class HttpClientFactory
{
    public static HttpClient Get()
    {
        var client = new HttpClient(
            new HttpClientHandler
            {
                MaxConnectionsPerServer = 100
            })
        {
            Timeout = TimeSpan.FromSeconds(20)
        };

        client.DefaultRequestHeaders.Add("Accept-Language", "ru");

        return client;
    }
}
Svirin
  • 564
  • 1
  • 7
  • 20
IceCreamVan
  • 192
  • 1
  • 10
  • 2
    `HttpClient is created for every request by this code` - not related to your problem, but you [should not do that](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/). Actually, it could be related to your problem if there is throttling of number of simultaneous connections. – GSerg Oct 21 '20 at 11:35
  • 2
    Its not a good idea to create a fresh Http client per request. See https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ – Klaus Gütter Oct 21 '20 at 11:35
  • Create a class variable as static that will contain instance of httpclient – Varun Setia Oct 21 '20 at 11:37
  • Have you checked this https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1 – Varun Setia Oct 21 '20 at 11:39
  • It can be related to multiple `HttpClient`'s or limited number of requests allowed per host by default. A little bit more info and links you can find in [this answer](https://stackoverflow.com/a/61957805/2501279) – Guru Stron Oct 21 '20 at 12:08
  • 1
    Please don't post code as an image. Text is always preferred. – Enigmativity Oct 21 '20 at 12:19

3 Answers3

0
public class MyClass
{

    static readonly HttpClient client = new HttpClient();
    //..rest of code
}

The instance created using static readonly modifiers will create one instance that will be valid throughout the application lifecycle and will not be modifiable.

Varun Setia
  • 165
  • 1
  • 11
0

The problem may be in MaxConnectionsPerServer. HttpClientHandler does not send a request if MaxConnectionsPerServer limit is reached, but Timeout starts to work from the moment of calling httpClient.SendAsync.

-2

I had a similar issue where I was loosing the HttpContext on async calls. The fix was to add

<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />

to the app settings in web.config. The post on SO is here.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rob P
  • 65
  • 1
  • 2
  • 7