views:

738

answers:

2

I'm trying to progressively download an array of serialised data. The goal is to send a single large block from the server, and partially process it on the client whilst it downloads.

I'm using the System.Net.WebClient class and setting it's AllowReadStreamBuffering property to false. According to the MSDN documentation, this should allow me to access the incoming stream from the OpenReadCompleted event.

When I attempt to access the stream, however, it throws a NotSupportedException. This is not a cross-domain policy issue, and if I set the AllowReadStreamBuffering property to true it loads and reads the content perfectly. Am I missing something? How should I perform progressive downloads from Silverlight?

The minimal code to replicate this problem is this:

    private void BeginProgressiveDownload()
    {
        WebClient client = new WebClient();
        client.AllowReadStreamBuffering = false;
        client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        client.OpenReadAsync(new Uri("http://STREAMABLE RESOURCE HERE"));
    }

    void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        e.Result.ReadByte();
    }
A: 

Are you on IE and downloading less than 4kb of data? IE won't give you the data until you have more than 4kb of it. After 4kb, you have all the granularity you need. Possible solutions:

  • Send garbage data to get up to 4kb
  • If you know the request is going to be small, set AllowReadStreamBuffering to true.
Erik Mork
A: 

Don't use WebClient for this, but rather sockets(if possible.)

Mike Schwarz has an excellent socket client you can use

http://weblogs.asp.net/mschwarz/archive/2008/03/07/silverlight-2-and-sockets.aspx

Jason Watts