ASP.net PageAsyncTaks - how to pass parameter to t

2019-07-24 09:10发布

I'm looking to make a series of web service calls (all to the same service, different paramters passed each time).

I've been doing a bit of reading about PageAsyncTasks, and they look right, but not sure how to pass a parameter down to each call to the service. Much simplified example below - real world will be looping around and changing the parameter before registering each task to be performed:

protected override void OnInit(EventArgs e)
{    
    base.OnInit(e);     
    int myParameter = 1;

    var task = new PageAsyncTask(BeginRequest, EndRequest, null, null, true);
    RegisterAsyncTask(task);    
} 

IAsyncResult BeginRequest(Object sender, EventArgs e, AsyncCallback cb, object state)
{    
    var service = new ServiceClient();    

    return service.BeginServiceCall(<How to get the parameter to the async call?>, 
                cb, service);
} 

... End request etc....

1条回答
放荡不羁爱自由
2楼-- · 2019-07-24 09:15

The 4th argument ("state" in our example) can be used to pass in an object as a parameter to your task. If you have more than one parameter that needs to be passed in, you'll want to create a seperate class (a DataTransferObject for example) that hold all of these parameters that would be used by your task.

查看更多
登录 后发表回答