在 asp.net-mvc 中,如何在不降低用户体验的情况下运行昂贵的操作? [英] In asp.net-mvc, how can I run an expensive operation without slowing down user experience?

查看:13
本文介绍了在 asp.net-mvc 中,如何在不降低用户体验的情况下运行昂贵的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 asp.net-mvc 网站,我的 ORM 使用的是 nhibernate.

I have an asp.net-mvc website and I am using nhibernate for my ORM.

我有一个执行基本 CRUD 更新的当前控制器操作(从数据库中查询一个项目,然后更新一堆值并提交回 db 表).然后它向客户端返回一个简单的 json 响应以指示成功或错误.

I have a current controller action that does a basic CRUD update (queries an item from the database and then updates a bunch of values and commits back to the db table). It then returns a simple json response to the client to indicate success or error.

 public ActionResult UpdateEntity(MyEntity newEntity)
 {
      var existingEntity = GetFromRepository(newEntity.Id);
      UpdateExistingEntity(newEntity, existingEntity);
      return Json(SuccessMessage);
 }

在某些情况下(假设提交成功并且如果我的对象中的某些字段发生了更改)我现在想触发一些额外的操作(例如给一群人发电子邮件并运行一些生成报告的代码)但我没有想要减慢正在进行更新的人的用户体验.所以我担心的是,如果我这样做:

In certain cases (assuming success of commit and if certain fields are changed in my object) I now want to trigger some additional actions (like emailing a bunch of people and running some code that generates a report) but I don't want to slow down the user experience of the person that is doing the update. So my concern is that if I did this:

 public ActionResult UpdateEntity(MyEntity newEntity)
 {
      var existingEntity = GetFromRepository(newEntity.Id);
      bool keyFieldsHaveChanged = UpdateExistingEntity(newEntity, existingEntity);
      if (keyFieldsHaveChanged)
     {
          GenerateEmails();
          GenerateReports();
     }
    return Json(SuccessMessage);
 }

对于某人更新的用户体验来说太慢了.无论如何(asyncc?)是否有一个昂贵的操作被控制器动作触发,但控制器动作不会因此而减慢?

that it would be too slow for the user experience of someone updating. Is there anyway (asyngc?) to have an expensive operation get triggered off of a controller action but not have that controller action slowed down because of it?

推荐答案

我以前做过这个.

最可靠的方法是使用异步控制器,或者更好的独立服务,例如 WCF 服务.

The most robust way would be to use Asynchronous Controller's, or better yet an independant service such as a WCF service.

但根据我的经验,我只需要像您说的那样做简单"的单线任务,例如审计或报告.

But in my experience, i've just needed to do "simple", one-liner task, such as auditing or reporting, as you say.

在那个例子中,简单的方法 - 启动一个 Task:

In that example, the easy way - fire off a Task:

public ActionResult Do()
{
    SomethingImportantThatNeedsToBeSynchronous();

    Task.Factory.StartNew(() => 
    {
       AuditThatTheUserShouldntCareOrWaitFor();
       SomeOtherOperationTheUserDoesntCareAbout();
    });

    return View();

}

这是一个简单的例子.您可以根据需要触发任意数量的任务、同步它们、在完成时获得通知等.

That's a simple example. You can fire off as many tasks as you want, synchronize them, get notified when they finish, etc.

我目前使用上述方法进行 Amazon S3 上传.

I've currently used the above to do Amazon S3 uploading.

这篇关于在 asp.net-mvc 中,如何在不降低用户体验的情况下运行昂贵的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆