实现一个进度条与ASP.NET MVC 2 AsyncController实现长期运行的任务 [英] Implementing a Progress bar for long running task implemented with an ASP.NET MVC 2 AsyncController

查看:248
本文介绍了实现一个进度条与ASP.NET MVC 2 AsyncController实现长期运行的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读AsyncControllers的文件之后的ASP.NET MVC 2中,我想知道是什么最好的方式来实现在这种情况下一个ajax的进度条。这似乎有点奇怪,该教程没有介绍这在所有。

After reading the documentation on AsyncControllers in ASP.NET MVC 2, I am wondering what's the best way to implement an ajax progress bar in this scenario. It seems a bit odd that the tutorial does not cover this at all.

我想实现一个AJAX进度条涉及要求返回当前任务状态的其他操作方法。不过,我不知道,交流工作线程和操作方法之间的任务状态信息的最佳方式。

I guess implementing an AJAX progress bar involves requires additional action method that returns the status of the current task. However, I am not sure about the best way to exchange information on the status of the task between worker threads and that action method.

我的最好的办法,到目前为止是把信息了短路电流进展到会话词典以及一个唯一的ID,并分享该ID的客户端,以便它可以查询的状态。但是,也许有更简单的方法,我没有注意到。

My best idea so far was to put information on the curent progress into the Session dictionary along with a unique id, and share that id with the client so that it can poll the status. But perhaps there is a much easier way that I did not notice.

什么是做到这一点的最好方法是什么?

What's the best way to do this?

谢谢

阿德里安

推荐答案

很有趣的问题!实际上它似乎不是 AsyncController 的任务。异步控制器设计用于在服务器端的长时间运行的单HTTP查询操作。当您使用异步操作,这只能帮助你一些长时间运行的操作(S)时发布ASP.Net工作线程,并允许它在服务的同时执行操作的其他请求。但从不要紧客户端点,这是异步的控制器或没有。对于客户端,这只是一个HTTP请求。​​

Very interesting question! Actually it seems that it is not a task for AsyncController. Async controllers are designed for long-running single-HTTP-query operations at server-side. When you are using async action, this could only help you to release ASP.Net worker thread during some long-running operation(s) and allow it to serve other requests while operation is performed. But from client-side point of view it doesn't matter, is this async controller or not. For client this is just single HTTP request.

您需要在您的应用程序中使用的一些长时间运行的查询服务,以重新设计这一点。下面是例子控制器,这样就能满足这样的工作流程:

You need to redesign this using some long-running queries service in your application. Here is example of controller, that could serve such workflow:

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;
    }
}

这是客户端的code,这可能与该控制器进行交互:

And here are client-side code, that could interact with this controller:

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
}

这是很基本的概念,也有一些遗漏的项目在这里,但我希望你能得到的主要思想。此外,它取决于你如何设计这个系统的组成部分,例如:

This is very basic concept, there are some missed items here, but I hope you will get the main idea. Also it's up to you how to design components of this system, for example:

  • 使用单为OperationsService, 或不;
  • 在那里经营了多久的结果应存储(DB?高速缓存? 会议)?;
  • 是不是真的需要手动释放资源并做什么时 客户端停止监视操作 (用户关闭浏览器)等。
  • use singleton for OperationsService, or not;
  • where and how long operation result should be stored (DB? Cache? Session?);
  • is it really required to manually release resources and what to do when client stopped to monitor operation (user closed browser) etc.

祝您好运!

这篇关于实现一个进度条与ASP.NET MVC 2 AsyncController实现长期运行的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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