简体   繁体   中英

Async method in asp.net web service

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.

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?

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?

Any articles would be appreciated also.

First, some background thoughts:

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) 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 .

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).

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

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.

You need to run your long-running process on a separate thread, so your web app doesn't seemingly go out to lunch when it's cranking out those reports. But you also want to report status on how far along you are in that process (via AJAX polling). I suggest your web service uses an object which handles this for you. Maybe something like this:

public class ReportGenerator {
  public string CurrentStatus;

  public void Run() {
    var worker = new Thread(new ThreadStart(this.ExportReports));
    worker.Start();
  }

  private void ExportReports() {
     this.CurrentStatus = "started";
     /* export a report */
     this.CurrentStatus = "1 report complete";
     /* export another report */
     this.CurrentStatus = "2 reports complete";
     /* etc, etc, etc */
  }
}

Your web service will need to be able to create and manage an instance of this object. Are you going to only allow one of these exports to happen at a time across the entire application? If so, use the Singleton Pattern for this object and make sure it's not already running before you start it again.

If multiple users can run this process simultaneously, you'll have to instantiate this object and store a reference to it in the user's session or something like that, so when you poll the web service, it can grab the object again and check its status, then return that information to the client. You can run into some really sticky situations with this type of threading, so be very careful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM