Skip to content

Instantly share code, notes, and snippets.

@huanlin
Last active March 19, 2020 07:15
Show Gist options
  • Save huanlin/7fc018f481fc7fb033c9ffd11138bb64 to your computer and use it in GitHub Desktop.
Save huanlin/7fc018f481fc7fb033c9ffd11138bb64 to your computer and use it in GitHub Desktop.
Demo deadlock with getting async result in ASP.NET 4.x
public class DemoDeadlockController : ApiController
{
[HttpGet]
public HttpResponseMessage DownloadPage()
{
var task = MyDownloadPageAsync("https://www.huanlintalk.com");
var content = task.Result;
return Request.CreateResponse($"網頁長度: {content.Length}");
}
private static HttpClient _httpClient = new HttpClient();
private async Task<string> MyDownloadPageAsync(string url)
{
string content = await _httpClient.GetStringAsync(url);
return content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment