views:

310

answers:

1

I need to make n number of async web service calls from an async ASPX page.

Each WS call retrieves a portion of a binary file. The code then outputs the file block to the page's response stream.

offset = 0;
blocksize = 1024;
output = getFileBlock(path, offset, blocksize);

//BinaryWrite output to Response

offset += blocksize;
output = getFileBlock(path, offset, blocksize);

//BinaryWrite output to Response

//etc...

Each getFileBlock is a web service call which I would like to make async. However, I need these calls to happen in a specific order.

Any suggestions how to implement this?

A: 

Call the async function. In the async completion handler, output the result, and if you still need more data, call the async function again.

public class Answer
{
  int _offset = 0;
  const int blocksize = 1024;
  string _path;

  static int main()
  {
    Init();
    getFileBlock(_path, _offset, _blocksize);
  }
  void function Init()
  {
    _path = "c:\foo.dat";
    CompletionEvent +=  HandleCompletion;
    getFileBlock(_path, _offset, _blocksize);
  }

  void function HandleCompletion( object sender, CompletionEventArgs e )
  {
    OutputResult( e.Result );
    _offset += blocksize;
    if ( _offset < limit )
    {
      getFileBlock(_path, _offset, _blocksize);
    }
  } 
}
Phillip Ngan
Phillip - I am calling this from an ASP.NET Asynchronous page. So there is no main(). I'm wondering how this would be done within ASP.NET.
frankadelic
I added the main() just for the sake of completeness, as a way of indicating that given the code, call Init(), and then call the async function getFileBlock(). You can call that code from an ASP.Net page.
Phillip Ngan