Search code examples
c#docker.net-coredotnet-httpclient

100 seconds timeout when accessing Microsoft Graph with HttpClient - but not with curl - inside Docker container


I am running a .NET 6 / ASP.NET Core application inside a Docker container on a Raspberry Pi.

Dockerfile to see .NET setup

This application is accessing Outlook and OneDrive using the Microsoft Graph SDK.

After really not touching the runtime environment it suddenly stopped working on Friday July 29th (so possibly a changed token behavior or alike on Azure Active Directory and/or Graph).

I removed the Graph layers of the application to break down the problem.

When I make a Graph call with HttpClient from this application / container

            var client = new HttpClient();
            client.Timeout = Timeout.InfiniteTimeSpan;
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.AccessToken);
            client.BaseAddress = new Uri("https://graph.microsoft.com");

            var response = await client.GetAsync("v1.0/me");
            var result = await response.Content.ReadAsStringAsync();

            return Ok(new HealthResult { Result = result });

I get a timeout after 100 seconds

2022-07-31T07:46:21.622984734Z fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
2022-07-31T07:46:21.623189265Z       An unhandled exception has occurred while executing the request.
2022-07-31T07:46:21.623227911Z       System.Threading.Tasks.TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing.
2022-07-31T07:46:21.623305047Z        ---> System.TimeoutException: A task was canceled.
2022-07-31T07:46:21.623336974Z        ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.

When I make the same call from inside this application / container with the very same access token over curl I get a response

            var process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "curl";
            process.StartInfo.Arguments = $"-H \"Authorization: Bearer {token.AccessToken}\" https://graph.microsoft.com/v1.0/me";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();

            string consoleResult = process.StandardOutput.ReadToEnd();
            return Ok(new HealthResult { Result = consoleResult });

like

{"result":"{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#users/$entity\",\"displayName\":\"John Doe\",\"surname\":\"Doe\",\"givenName\":\"John\",\"id\":\"1234567890abcdef\",\"userPrincipalName\":\"john.doe@outlook.com\",\"businessPhones\":[],\"jobTitle\":null,\"mail\":null,\"mobilePhone\":null,\"officeLocation\":null,\"preferredLanguage\":null}"}

What I already tried

What do I miss here? I will keep on dissecting the problem further and would be really thankful for any hints.


Solution

  • HttpClient has a default timeout of 100 seconds. You can read about it at MS documentation.

    If you wanted to setup Infinite time then set it as shown below

    httpClient.Timeout = TimeOut.InfiniteTimeSpan;