Profile c# Async await tasks with dotnet agent
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;
}
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.
Please sign in to leave a comment.
Comments
0 comments