views:

21

answers:

3

http://msdn.microsoft.com/en-us/library/system.net.openwritecompletedeventargs.result(VS.95).aspx

I'm writing a .NET app (Silverlight API) using the WebClient class. I'm simply wanting to get an XML-style result of a server script after uploading a file Async. I figured the Result stream inside of OpenWriteCompletedEventArgs would be what I would want to look at, but MSDN says Result is: "Gets a writable stream that is used to send data to a server.".. I honestly fail to see how you can write once completed. I wonder if I can actually read from this, and if I can't, how do I go about reading the server message after the upload?

static void UploadCompleted(object sender, OpenWriteCompletedEventArgs e)
{
   /* e.Result says its only a writable stream */
}
A: 

No, you don't read from this. The point of this event is to let you write the body of an HTTP request to the server. If you don't need to write any data in your request, you don't need this event. If you need to write data, you'd use this event and then read the result when later events fire.

If you need to read, you want to do it when a read event has completed - e.g. OpenReadCompleted.

Jon Skeet
Will OpenReadCompleted be invoked by OpenWriteAsync?
dtb
@dtb: It's not clear to me, to be honest. The docs say that when you close the stream, the thread will block until the result is ready... but that sounds a little odd too.
Jon Skeet
I'll investigate it further.. Unfortunately I have about 5,000 more lines out of 35,000 to port to .NET from Obj-C before this even runs. I'll certainly post my findings here
Kyle
A: 

The documentation could be ambiguous or you are not using the correct method. Here's an example:

client.UploadFileAsync(new Uri("http://example.com"), @"c:\work\foo.txt");
client.UploadFileCompleted += (sender, e) =>
{
    // e.Result is a byte[] containing the server response 
    // which in your case is XML
};
Darin Dimitrov
Unfortunately this isn't in the current Silverlight subset of the .NET api. Wish it was! Unless it's somewhere else other than WebClient?
Kyle
A: 

The OpenWriteCompleted event is invoked when the HTTP connection to the server has been established, not when the upload has finished. You write to the stream to upload your data and close the stream when you're done.

There doesn't seem a way to obtain the response from the server when using OpenWriteAsync.

dtb