Skip to content

Getting HTTP stream is blocking rendering #13329

@enetstudio

Description

@enetstudio

I'm downloading a stream from a WebAPI and I'm trying to run code while the stream is being read.

The code below works fine on Blazor server-side or an console app. But on blazor-client side the stream will first download fully before executing any code.... even just a Console.WriteLine() will just start writing to Console after the stream is done. I'm using the latest .Net Core 3.0 Preview 8...

public async IAsyncEnumerable<SomeClass> GetDataAsync()
{
    var serializer = new JsonSerializer();
    using (var stream = await Http.GetStreamAsync("https://localhost:44334/api/values/i"))
    {
        using (var sr = new StreamReader(stream))
        using (var jr = new JsonTextReader(sr))
        {
            while (await jr.ReadAsync()) //<-- Stream is fully download to browser before taking any actions.
            {
                if (jr.TokenType != JsonToken.StartArray && jr.TokenType != JsonToken.EndArray)
                {
                    Console.WriteLine(jsonReader.LinePosition);
                    yield return serializer.Deserialize<SomeClass>(jr);
                }
            };
        }
    }
}

I tested this with WireShark to confirm that this is happening.

Is this by design?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions