What is the current and recommended way to smooth an asynchronous stream?

I have an ASP.NET MVC 3 (.NET 4) web application.

I have an action method [HttpPost]that passes some data to the database.

Now, after this method finishes working with the repository, I want to perform a "background" task (think about auditing or sending emails, etc.), where I do not need the result (if there is no error, in which case I I will perform logging).

How can I run this task from my action method?

[HttpPost]
[Authorize]
public ActionResult Create(MyViewModel model)
{
   if (ModelState.IsValid)
   {
      _repo.Save(model); 
      // TODO: Fire off thread
      return RedirectToRoute("Somepage", new { id = model.id });
   }
   return View(model);
}
+5
source share
4 answers

A new .NET 4 way to do this is with Task.

http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx

Task.Factory.StartNew(MyBackgroundAction);

, , ThreadPool.

ThreadPool.QueueUserWorkItem(MyBackgroundAction)

- .

, / .

+4

, ASP.Net .

, , . , , , , , . , , (, -).

, . 100 -, .

, . , . , . , , .

MSMQ.

+2

Maybe you should consider a service bus, for example nServiceBus or use MSMQ, as described here - http://dotnetslackers.com/articles/aspnet/Sending-email-from-ASP-NET-MVC-through-MVC-and-MSMQ- Part1.aspx

+1
source
// Fire off thread
var t = new System.Threading.Thread(() =>
{
   // do whatever
});
t.Start();

In "do whatever" you should try / catch what logs any exceptions.

0
source

All Articles