0

I have an ASP.NET MVC app written in C#. One of my actions looks like this:

[HttpPost]
public async Task<ActionResult> Save(int id, MyViewModel viewModel)
{
  viewModel.SaveToDb();

  await Backup.Save(viewModel);
  return View(viewModel);
}

...

public class Backup
{
  async public static Task Save(IBackup item)
  {
    // do stuff
  }
}

There is actually a lot going on in the Backup.Save function. In fact, await Backup.Save(...) is currently taking 10 seconds. For that reason, I want to run it in the background (asynchronously) and not (a)wait on it. I thought if I just removed the await keyword, it would work. However, it does not appear that is the case.

How can I run Backup does save asynchronously in the background so that my users can continue using the app without long wait times?

Thank you!

JQuery Mobile
  • 6,221
  • 24
  • 81
  • 134
  • 2
    You need a reliable method for creating background job in ASP.NET, as described [here](http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx). – mason Jul 14 '15 at 19:05
  • Learn about TPL, task.run(), – Gilad Jul 14 '15 at 19:06
  • 1
    that's not what async/await are for – Jonesopolis Jul 14 '15 at 19:06
  • 1
    @Gilad Learn about ASP.NET. You can't reliably use Task.Run in ASP.NET, you can get your AppDomain shutdown before your task completes. Think like how a console application behaves if you do a Task.Run in Main() without waiting for it to finish, your program will shutdown before the task finishes. IIS does the same thing, once the response is sent back it consideres the program as "done" and may shutdown the AppDomain killing all background threads. – Scott Chamberlain Jul 14 '15 at 19:26
  • Ohh my bad, good to know – Gilad Jul 14 '15 at 19:42

2 Answers2

2

See here for a discussion of this: Can I use threads to carry out long-running jobs on IIS?

One simple alternative without having to use threads or queues would be to make an additional async call via the client, possibly via Ajax assuming the client is a browser.

If you need the view model back immediately, then potentially split this into two methods. One that gets the view model, and then you call a second method that does the save in the background.

This should work fine since if you are happy to return right away, you have already implicitly agreed to decoupled the dependency between the two actions.

It is a bit of a hack, since the client is now requesting an action it may not need to know or care about.

Community
  • 1
  • 1
Nitesh Patel
  • 631
  • 3
  • 10
0

you can make a method that takes an action or a function (whatever you need), and runs a thread with whatever you need to run in the background.

Basically whatever way you choose to make it, run the method in a separate thread.

Giora Guttsait
  • 1,279
  • 15
  • 29
  • 1
    Just "Running a thread" in ASP.NET is more complicated than that. IIS will shut the AppDomain down on you before your task completes. – Scott Chamberlain Jul 14 '15 at 19:24
  • Read the SO question [Can I use threads to carry out long-running jobs on IIS?](http://stackoverflow.com/questions/536681/can-i-use-threads-to-carry-out-long-running-jobs-on-iis) for more info. – Scott Chamberlain Jul 14 '15 at 19:27