Profile c# Async await tasks with dotnet agent

Community post

Shared by the Applicare community. It has not been re-checked against Applicare 10.0, so confirm the steps in your environment or contact Support.

Applicare dotnet agetn can profiler and give you the detailed method level traces of transactions with async await method.

Consider the following sample code

        public string GetAsync()
        {
            var httpClient = new HttpClient();

            var Response = httpClient.GetAsync("http://localhost:84/api/values/get");
            var Result = Response.Result;
            string text = Result.Content.ReadAsStringAsync().Result;

            return text;
        }

        public string GetByteArrayAsync()
        {
            var httpClient = new HttpClient();

            var Response = httpClient.GetByteArrayAsync("http://localhost:84/api/values/get");
            var Result = Response.Result;

            string text = Encoding.UTF8.GetString(Result);

            return text;
        }

        public string SendAsync()
        {
            var httpClient = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage();
            request.RequestUri = new System.Uri("http://localhost:84/api/values/get");
            CancellationToken token = new CancellationToken();

            var Response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, token);
            var Result = Response.Result;
            string text = Result.Content.ReadAsStringAsync().Result;

            return text;
        }

[Screenshot 1 is not available on this page.]

Above is the stack trace captured in appliare for the above code. HttpClient's GetAsync is a asynchronous method. Its invocation will end immediatley and process the web request in a new thread. So in the above trace the external web services call shows 0 ms. But the Asynchronous task shows the exact time taken to process this request. This trace will help us to understand when the async invocation is triggered and when the task gets completed.

Join the discussion

Comments, voting and following are kept by Zendesk and need a Help Center account. This button opens the original discussion there.

Opens Zendesk · needs internet and sign-in

Still stuck? Contact support