I'm trying to confirm that a library I wrote will not cause an asychronous deadlock. To do that, I'm first trying to intentionally cause one (so that I can replace the innermost call with a call to my library), but no matter what I do, no deadlock occurs... what am I misunderstanding?
I've setup the relevant code in a new api (because I thought that apis were non-reentrant/single-threaded) and just in case it matters, I've moved each function to its own class and added Thread.Sleep commands to ensure that there is activity in each method both before and after the await.
public class TestingController : ApiController
{
[HttpPost]
[Route("api/Testing/TriggerAsyncDeadlock")]
[ResponseType(typeof(string))]
public HttpResponseMessage TriggerAsyncDeadlock()
{
return Request.CreateResponse(HttpStatusCode.OK, new AsyncCaller().CallerAsync().Result);
}
}
public class AsyncBase
{
public async Task<string> BaseAsync()
{
Thread.Sleep(500);
var ret = await Task.FromResult("Result");
Thread.Sleep(500);
return ret;
}
}
public class AsyncCaller
{
public async Task<string> CallerAsync()
{
Thread.Sleep(500);
var ret = "These are the results: ";
var asyncBase = new AsyncBase();
for (var i = 1; i < 5; i++)
{
ret += $"'{await asyncBase.BaseAsync()}',";
Thread.Sleep(500);
}
return ret.Substring(0, ret.Length - 1);
}
}
Edit: I'm running .net framework 4.7.1