加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用ASP.NET MVC 2 AsyncController实现长时间运

发布时间:2020-12-15 23:40:20 所属栏目:asp.Net 来源:网络整理
导读:在ASP.NET MVC 2中阅读 documentation on AsyncControllers之后,我想知道在这种情况下实现ajax进度条的最佳方法是什么.这个教程似乎有点奇怪, 我猜想实现一个AJAX进度条需要额外的操作方法来返回当前任务的状态.但是,我不知道在工作线程和该操作方法之间交换
在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?
会议)?;
>是否真的需要手动释放资源以及何时执行
客户端停止监视操作
(用户关闭浏览器)等

祝你好运!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读