MVC5 - Deadlock with async Tasks? - asp.net-mvc

I think my web application is suffering from deadlock when I'm calling YouTube API services, so I want to know how to resolve this in the correct manner. I suspect it's a similar scenario to the following: Why does this async action hang?
Please can somebody advise, in very simple terms, why my web application hangs (see inline comments) and how it should be correctly resolved? Thanks!
public ActionResult Index()
{
YouTubeHelper yth = new YouTubeHelper();
bool unpublishVideo = yth.UpdateVideoOnYouTube(17, "public").Result;
}
public async Task<bool> UpdateVideoOnYouTube(int propertyId, string publishStatus)
{
.....
YouTubeService youtubeService = await GetYouTubeService(db);
.....
}
public async Task<YouTubeService> GetYouTubeService(ApplicationDbContext db)
{
....
if (!await credential.RefreshTokenAsync(CancellationToken.None)) //It hangs here!!
{
....
}
....
}

The deadlock is explained here. In summary, your asynchronous methods are needing the ASP.NET request context before they can complete, but the call to Result is blocking the ASP.NET request context until the asynchronous methods are already completed.
To avoid the deadlock, don't block on async code. Use await instead of Result:
public async Task<ActionResult> Index()
{
YouTubeHelper yth = new YouTubeHelper();
bool unpublishVideo = await yth.UpdateVideoOnYouTube(17, "public");
}

Related

.Net MVC - Background process implementation with HostingEnvironment.QueueBackgroundWorkItem

I am looking for something like a background process in MVC.
Requirement is I should run a background process/thread in .Net MVC application, which will be async and doesn't wait for the action in the foreground/ UI. Similar to fire and forget.
I am trying to use HostingEnvironment.QueueBackgroundWorkItem for the same. Can this be used or anything else is recommended ? I am armature in MVC, Thanks in advance.
If you want to use background job in controller HostingEnvironment.QueueBackgroundWorkItem is normal solution.
For examle, start backgroud action and foget
public class HomeController : Controller
{
[HttpPost]
public ActionResult Index()
{
Action<CancellationToken> workItem = SomeBackGroundWork;
// start background work and forget
HostingEnvironment.QueueBackgroundWorkItem(workItem);
return RedirectToAction("Home");
}
private async void SomeBackGroundWork(CancellationToken cancellationToken)
{
await Task.Delay(2000,cancellationToken);
// or you can do http request to web site
using (var client = new HttpClient())
{
var response = await client.PostAsync("http://google.com", new StringContent(""), cancellationToken);
}
}
}

Mark page as "async = true" for a Visual Studio MVC Project

I am using Edge.js so that I can call Node.js from C#. According to the documentation in the link I would do that similar to the following:
[HttpPost]
public ActionResult Input(InputModel obj)
{
validateInput(obj);
return View();
}
private async void validateInput(object obj)
{
var func = Edge.Func(#"
return function (data, callback){
var username = data.username,
email = data.email;
callback(null, username);
}
");
ViewBag.Msg = (string)await func(obj);
}
However, I get the following run time error:
Additional information: An asynchronous operation cannot be started at this time.
Asynchronous operations may only be started within an asynchronous handler or module or during
certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the
Page is marked <%# Page Async="true" %>. This exception may also indicate an attempt to call an
"async void" method, which is generally unsupported within ASP.NET request processing. Instead,
the asynchronous method should return a Task, and the caller should await it.
My question is two-fold:
1.How do I make the page, async=true. I know how to do this for a web forms project but not a MVC project.
2.Is there a better way to do what I am trying to do? A red flag will probably go up when you see that I am returning void however this is do to the fact that Edge.js is being used. Even so, I have tried returning a Task and then task.Wait() in the calling method but the task never finishes.
After trying some different things, the following solution worked for me.
Even though I answered my own question, and it seems trivial, I am not removing this question as there are not a lot of knowledge on the web about Edge.js.
[HttpPost]
public async Task<ActionResult> Input(InputModel obj)
{
ViewBag.Msg = await validateInput(obj);
return View();
}
private async Task<string> validateInput(object obj)
{
var func = Edge.Func(#"
return function (data, callback){
var username = data.username,
email = data.email;
callback(null, username);
}
");
return (string)await func(obj);
}

Not able to return ActionResult from a controller which calls async method

I am working on a sample app to query an api and I am trying to move the generic piece into QueryAsync which queries and returns Task. This function will be called from a controller action GetData which can returns Task too. Here, I am awaiting while retrieving the result from QueryAsync. I feel it is not required as QueryAsync is not returning an awaitable object. But then if I don't, i will not be able to return ActionResult from GetData controller. Is my thinking and implementation here correct? Sorry, but I am new to using Async with MVC.
private async Task<ActionResult> QueryAsync<T>(string url)
{
Task<string> response_task = HttpClientService.HttpClientService.GetRequest(url);
IEnumerable<T> model = JsonConvert.DeserializeObject<IEnumerable<T>>(await response_task);
return View(model);
}
//
// GET:
public async Task<ActionResult> GetData()
{
string _address = "https://someurl";
return await QueryAsync<ClassType>(_address);
}
You are returning an awaitable Task from QueryAsync, which is a Task that "asynchronously" runs GetRequest, then synchronously deserializes the result and returns an ActionResult. Think of those operations as a whole, squeezed into an awaitable Task.
From async/await API perspective, your implementation is correct. Your implementation is equivalent to the following (ignoring generic method):
public async Task<ActionResult> GetData()
{
var url = "https://someurl";
var response = await HttpClientService.HttpClientService.GetRequest(url);
var model = JsonConvert.DeserializeObject<IEnumerable<ClassType>>(response);
return View(model);
}

should I use await on return?

I have an MVC web application, the controller action calls a code in a business layer, which in turn calls an API:
public async Task<ActionResult> TestAction(){
Info something = await businessLayer.GetInfoAsync();
return View(something);
}
in the business layer I have GetInfoAsync defined as follows:
public async Task<Info> GetInfoAsync(){
return await myLib.GetAsync<Info>();
}
in my library I have GetAsync defined as follows:
public async Task<T> GetAsync(){
//do something
var data = await aService.Get<T>();
return data;
}
As you can see above GetInfoAsync method just makes a call to myLib.GetAsync and returns whatever value myLib.GetAsync returns.
Could I just get rid of async/await for GetInfoAsync, and define it as follows :
public Task<Info> GetInfoAsync(){
return myLib.GetAsync<Info>();
}
Thanks !
Yes, you can (and should) get rid of async/await in the middle layer.
If your async method is just awaiting a single task (and that's all it does), then the async just adds overhead.

Writing to a HttpResponse with BufferOutput=false blocks when an ActionResult comes from a ASP.NET MVC 4 asynchronous action and Glimpse is enabled

I have a custom ActionResult that sets HttpResponse.BufferOutput = false and then writes data to the response stream. I noticed that when the action result comes from a task-based asynchronous action method in ASP.NET MVC, writing to the response stream blocks. This happens only when Glimpse plugin is enabled in web.config. Glimpse is very useful to me, I really want to have it enabled, at least during development and testing. BufferOutput property should remain false, because the content length can be quite large and I don't want to buffer it in memory.
This is the shortest code that could reproduce this exact behaviour:
public sealed class CustomResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
var resp = context.HttpContext.Response;
resp.BufferOutput = false;
resp.ContentType = "text/plain";
resp.Output.Write(DateTime.UtcNow.ToString());
resp.Flush();
}
}
public sealed class DownloadController : Controller
{
// the client nevers gets the response from this action
public async Task<ActionResult> Async()
{
await Task.Yield();
return new CustomResult();
}
// this works
public ActionResult Sync()
{
return new CustomResult();
}
}
I tested this with the latest Glimpse.Mvc4 package (version 1.3.2).
Am I doing something wrong, is there a workaround to this issue or is this a Glimpse bug and I should report it?

Resources