views:

1027

answers:

4

Probably a long question for a simple solution, but here goes...

I have a custom made silverlight control for selecting multiple files and sending them to the server. It sends files to a general handler (FileReciever.ashx) using the OpenWriteAsync method of a WebCLient control.

Basically, the silverlight code does something like this for each file:

WebClient client = new WebClient();
client.OpenWriteCompleted += (sender, e) =>
  {
    PushData(data, e.Result);
    e.Result.Close();
    data.Close();
  };
client.OpenWriteAsync(handlerUri);

The server side handler simply reads the incoming stream, and then does some more processing with the resulting byte array.

THE PROBLEM is that client side OpenWriteCompleted is done as soon as all the data has been sent over the wire. My code will then contine with the next file. What I really want is to wait until the ASHX handler has finished with all it's processing of that request. How do I do that? Any wait mechanism on WebClient? Any callback I can do on the HttpContext in the handler? Should I use some other kind of transfer technique? Please advice!

A: 

Hrm, maybe a simple solutioin could be to tag the url with a GUID(the guid being unique per file, or transfer, whatever makes sense to your situatuation). Then you can have another simple web service that is capable of checking on the status of the other service, based on the guid, and have your silverlight client query that new service for its processing status(by passing the new web service the guid of the past transfer).

mattlant
Something like that would probably work - but I hope there is another way, somthing built in, not involving a separate service like that.
Torbjørn
A: 

I'm assuming that you're concerned that the data being returned from the handler is taking a long time to transfer and the server is not being utilized during that time. There isn't a way to tell when the server is done processing, so I don't think you can do this without changing your architecture.

I would have your handler only an identifier of some sort (like a GUID or int) that can be used to retrieve the result of the handler in another request. So the page would call the handler, the handler would store the result and return the identifier, the page would call the handler the second time and call another handler to get the result of the first call. This would keep your server in use while your data was transferring.

Dan Goldstein
I'm concerned that the server side processing takes a lot of resources, so I don't want to send file number two before I know that file number 1 if completely processed (not only recieved) by the server. You're answer however is basically the same as mattlant's, so guess that's the way I have to go.
Torbjørn
A: 

Or you can probably do it with JavaScript (jQuery)... if you don't mind using JavaScript that is.

ANaimi
A: 

If files are not very big, and is feasible to keep each of them in memory, an ugly yet effective solution is converting them to strings and sending them using the UploadStringAsync method.

Avoid this approach if file size is unbounded, but if you can now that they will be relatively small, it is possible to use this approach.

Santiago Palladino