Problem with repetative Http request

Hi,

I have a project where I want to send and receive information to an Api server in my own network.
This is the code on my meadow:
class TPProductieService
{

    public TPProductieService() 
    {
        PingHttpClientAsync().Wait();
    }



    private static async Task PingHttpClientAsync()
    {
        const string serverUri = "Https://192.168.182.3/";
        var counter = 0;
        int breedte = 220;

        while (true)
        {
            counter++;

            using (HttpClient client = new HttpClient())
            {
                client.Timeout = new TimeSpan(0, 1, 0);

                //HttpRequestCachePolicy noCachePolicy =
                    //new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

                var request = new HttpRequestMessage
                {
                    Method = HttpMethod.Get,
                    RequestUri = new Uri(serverUri + "Productie/GetMeters/"+breedte),
                    Headers =
                    {
                        Connection = { "keep-alive"}
                    }
                };

                var response = await client.SendAsync(request);
                 
                Console.WriteLine("Response : " + await response.Content.ReadAsStringAsync() + " " + counter + " times");
                response.Dispose();
                request.Dispose();
            }
            Thread.Sleep(2000);
        }
    }
}

And this is the Api:

    [System.Web.Http.Route("Productie/GetMeters/{breedte}")]
    public async Task<IHttpActionResult> GetMeters(int breedte)
    {
        return Ok(breedte);
    }

After repeating about 193 times, depending on the Tread.sleep time, the meadow just stops responding.

Does anyone know what I am doing wrong here?

Thanks in Advance,

Martijn.

Try putting the instantiation of the HttpClient outside of the while loop.

This is the recommended advice for ASP.NET applications and various people have documented problems with the using pattern and HttpClient

Regards,
Mark

Hi Mark,

Thanks for your reply. The weird thing is, when I do that, it takes several minutes (2 minutes to be precise) to get a response.
The using pattern is the only way to get a quick response.

public async Task PingHttpClientAsync()
{
var counter = 0;

        HttpClient httpClient = new HttpClient();

        while (true)
        {
            counter++;
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri(serverUri + "Productie/GetMeters/" + counter),
                Headers =
                {
                    Connection = { "keep-alive"}
                }
            };
            try
            {
                var response = await httpClient.SendAsync(request);

                Console.WriteLine("Response : " + await response.Content.ReadAsStringAsync() + " times");
                response.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            request.Dispose();              
        }
    }
}

This is what happens:

The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core.
The program ‘[4860] App.dll’ has exited with code -532462766 (0xe0434352).
[15-10-2021 14:58:53] Meadow StdOut: Initializing file system…
[15-10-2021 14:58:55] Meadow StdOut: File system initialized.
[15-10-2021 14:58:55] Meadow StdOut: Initialize hardware…
[15-10-2021 14:58:56] Meadow StdOut: Connecting to WiFi Network trajectparket
[15-10-2021 14:59:27] Meadow StdOut: Connected. IP: 192.168.182.141
[15-10-2021 14:59:39] Meadow StdOut: Response : 1 times
[15-10-2021 15:01:42] Meadow StdOut: Response : 2 times
[15-10-2021 15:03:45] Meadow StdOut: Response : 3 times
[15-10-2021 15:05:53] Meadow StdOut: Response : 4 times
[15-10-2021 15:07:52] Meadow StdOut: Response : 5 times
[15-10-2021 15:09:58] Meadow StdOut: Response : 6 times
[15-10-2021 15:12:00] Meadow StdOut: Response : 7 times
[15-10-2021 15:13:58] Meadow StdOut: Response : 8 times
[15-10-2021 15:16:01] Meadow StdOut: Response : 9 times

Maybe I am missing a detail somewhere?

Martijn