在asp.net web服务异步方法 [英] Async method in asp.net web service

查看:140
本文介绍了在asp.net web服务异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产生了一堆的报告和电子邮件结果的拉链的页面。这个过程大约需要30秒到2分钟。

I've got a page that generates a bunch of reports, and emails a zip of the results. This process takes about 30sec to 2min.

当用户通过AJAX调用这个过程中,我可以通过轮询AJAX Web服务得到的结果,但我要如何创建一个异步方法,将不断更新变量?

When the user invokes this process via ajax, I can poll the web service via ajax to get the results, but how do I create an async method that will continuously update a variable?

为了简单起见,我怎么能写会从1数到1万元,每个号码添加到列表,并在同一时间能够查询Web服务和表示列表中返回项目的当前数目的方法?

For simplicity, how could I write a method that would count from 1 to 1 million and add each number to a list, and at the same time be able to poll the web service and return the current number of items in said list?

任何物品将AP $ P $也pciated。

Any articles would be appreciated also.

推荐答案

首先,一些背景想法:

1),因为它是一个长期运行的工作,你可能需要$ P $的缓存pviously执行的工作,这样你不会重复工作和负担过重的服务器。

1) Since it's a long running job, you may want a cache of previously executed jobs so that you aren't duplicating work and overburdening your server.

2)你不应该启动一个单独的后台线程,因为每个ASP.NET请求将是在它自己的线程反正执行。但是,如果你发现自己正在挨饿的ASP.NET工作进程,可以让你的Web方法在服务器端异步 - 看到:的这篇文章

2) You shouldn't have to start a separate background thread, since each ASP.NET request is going to be executing in it's own thread anyway. However, if you find you are starving your ASP.NET worker process, you can make your web methods asynchronous on the server side - see: this article.

的实施需要三个组成部分 - 高速缓存,一个WebMethod运行作业,和网络的方法来检查作业状态。此外,还需要能够产生一个密钥,它允许系统跟踪的请求。您可以生成该服务器端(当你生成页面),或客户端(如果你有一个聪明的算法,可以生成唯一的ID)。

The implementation requires three components - a cache, a webmethod to run the job, and a web method to check job status. You will also need to be able to generate a key that allows the system to track requests. You can generate this server side (when you generate the page), or client side (if you have a clever algorithm that can generate unique ids).

缓存可以应用范围,如果你希望用户能够共享运行的作业,或会话范围的,如果你不这样做。缓存是简单地保存到您正在运行的作业实例的引用的字典。

The cache can be application scoped if you want users to be able to share running jobs, or session scoped if you don't. The cache is simply a dictionary that holds references to your running job instances.

一个简单的例子(这不会编译,可能包含语法错误,并拟主要是为了传达出点)。

A simple example (this won't compile, probably contains syntax errors, and is intended mostly to get the point across).

服务器伪code:

//In application Session_Start, 
Session["ReportCache"] = new Dictionary<string, ReportStatus>();

public class ReportResults
{
}

public class ReportStatus
{
    public int PercentComplete = 0;
}

[WebMethod(EnableSessions = true)]
ReportResults RunReport(string uniqueid)
{
    ReportStatus status = new ReportStatus();
    Session["ReportStatus"].Add(uniqueid, status);
    //Start report
    for(int i = 0; i < 100000; ++i)
    {

        //update the report status
        status.PercentComplete = 100 * 100000 * i / 100000;
    }
    Session["ReportStatus"].Remove(uniqueid);

}

[WebMethod(EnableSessions=true)]
public ReportStatus GetStatus(uniqueid)
{
    try
    {
        return Session["ReportStatus"][uniqueid];
    }
    catch(Exception)
    {
        return null;
    }
}

在客户端,使用AJAX调用第一个Web方法。直到报告完成后它不会调用的onSuccess回调。使用的setTimeout定期轮询第二个Web方法获取状态信息和更新客户端。当第一个Web方法完成,取消计时器。

On the client, use ajax to call the first web method. It won't call the onSuccess callback until the report is finished. Use setTimeout to periodically poll the second web method to get status information and update the client. When the first web method completes, cancel your timer.

您最大的问题将是您的服务器将是运行大量的长时间运行的作业,它可以降低整体性能和响应能力的事实。你可能希望完全采取不同的方法。例如,而不是运行在每个用户的需求报告,可以排队谁想要得到的报告人名单 - 那么,一次一个小时/天/周(不管)你可以有一个单独的进程运行报告一次并发送出去。

Your biggest problem is going to be the fact that your server is going to be running lots of long running jobs, which can degrade overall performance and responsiveness. You might want to take a different approach altogether. For instance, rather than running the reports on demand per user, you can queue up the list of people who want to receive the report - then, once an hour/day/week (whatever) you can have a separate process run the report once and send it out.

这篇关于在asp.net web服务异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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