asp.net-mvc – 使用ASP.NET MVC 2 AsyncController实现长时间运行的任务的进度条

在ASP.NET MVC 2中阅读 documentation on AsyncControllers之后,我想知道在这种情况下实现ajax进度条的最佳方法是什么.这个教程似乎有点奇怪,

我猜想实现一个AJAX进度条需要额外的操作方法来返回当前任务的状态.但是,我不知道在工作线程和该操作方法之间交换有关任务状态的信息的最佳方式.

到目前为止,我最好的想法是将信息进入会话字典和唯一的ID,并与客户端共享该id,以便它可以轮询状态.但也许有一个更简单的方法,我没有注意到.

最好的方式是做什么?

谢谢,

阿德里安

解决方法

非常有趣的问题!实际上它似乎不是AsyncController的任务.异步控制器设计用于在服务器端长时间运行的单HTTP查询操作.当您使用异步操作时,这只能帮助您在某些长时间运行的操作期间释放ASP.Net工作线程,并允许在执行操作时提供其他请求.但从客户端来看,这并不重要,这是不是同步控制器吗?对于客户端,这只是单个HTTP请求.

您需要在应用程序中使用一些长时间运行的查询服务来重新设计.以下是控制器的示例,可以为此工作流提供服务:

public class LongOperationsController : Controller
{
    public ActionResult StartOperation(OperationData data)
    { 
        Guid operationId = Guid.NewGuid(); // unique identifier for your operation
        OperationsService.DoStartOperation(operationId,data); // service starts to perform operation using separate thread
        return new JsonResult(operationId); // operation id should be sent to client to allow progress monitoring
    }

    public ActionResult GetoperationStatus(Guid operationId) 
    {
        var status = OperationsService.GetStatus(operationId); // this method returns some object,that describes status of operation (e.g. progress,current task etc.)
        return new JsonResult(status); // returning it to client
    }

    public ActionResult GetoperationResult(Guid operationId)
    {
        var result = OperationsService.GetoperationResult(operationId); // this should throw exception if operation is not yet completed
        return new JsonResult(result);
    }

    public ActionResult ClearOperation(Guid operationId)
    {
        OperationsService.ClearOperationResult(operationId); // we should delete operation result if it was handled by client
        return true;
    }
}

这里是客户端代码,可以与此控制器进行交互:

var operationId;
function startOperation(data) {
    $.post('/LongOperations/StartOperation',data,function(response) {
        operationId = response; // store operationId
        startOperationMonitoring(); // start
    },'json');
}

function startOperationMonitoring() {
    // todo : periodically call updateOperationStatus() to check status at server-side
}

function updateOperationStatus() {
    // todo : get result of GetoperationStatus action from controller 
    // todo : if status is 'running',update progress bar with value from server,if 'completed' - stop operation monitoring and call finishOperation()
}

function finishOperation() {
    // todo : get result of GetoperationResult action from controller and update UI
    // todo : call ClearOperation action from controller to free resources
}

这是非常基本的概念,这里有一些错过的项目,但我希望你会得到主要的想法.还有,您如何设计该系统的组件,例如:

>为OperationsService使用单例,
或不;
>应该存储运算结果的位置和时间(DB?Cache?
会议)?;
>是否真的需要手动释放资源以及何时执行
客户端停止监视操作
(用户关闭浏览器)等

祝你好运!

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....