I've tested in a clean .net core mvc, the following code is working as expected:
The view is returned to the client immediately and the sleep is still running and we can see log from the console after the sleep period.
public IActionResult Index()
{
try
{
_logger.LogInformation($"Index called");
return View();
}
finally
{
_logger.LogInformation($"Return done, first line in finally");
Response.OnCompleted(async () =>
{
_logger.LogInformation($"In complete callback, going to sleep");
await Task.Run(() => Thread.Sleep(7000));
_logger.LogInformation($"sleep done");
});
}
}
However if we put the same controller & view in a plugin admin page, the view is not returned until the sleep is done. The page is just an ordinary admin config page.
@{
Layout = "_AdminLayout";
//page title
ViewBag.Title = "A";
//active menu item (system name)
Html.SetActiveMenuItemSystemName("A");
}
My observation is that this is a partial view that wrapped in the AdminLayout, but I think it should not affect the OnComplete handler, please help, thanks in advance.