mono / mono

Mono open source ECMA CLI, C# and .NET implementation.
https://www.mono-project.com
Other
11.11k stars 3.82k forks source link

Cannot create MemoryStream in blazor (wasm) #19907

Open zxyao145 opened 4 years ago

zxyao145 commented 4 years ago

Describe the bug

I'm trying to read a file in blazor (wasm), but I get an error result. All MemoryStream bytes are 0. The code is as follows, and I'm sure the bytes have data (I printed them in the console).

To Reproduce

private async Task<Stream> CreateMemoStream(byte[] bytes)
{
    Stream s = new MemoryStream();
    await s.WriteAsync(bytes);
    s.Position = 0;
    await s.FlushAsync();
    //debug
    var br = new BinaryReader(s);
    var bytes2 = new byte[s.Length];
    br.Read(bytes2);
    Console.WriteLine(string.Join(',', bytes2));// here is "0,0,0,...,0"
    return s;
}

Exceptions (if any)

Further technical details

marek-safar commented 4 years ago

/cc @lewing

lewing commented 4 years ago

@tqiu8 can you try to reproduce this?

tqiu8 commented 4 years ago
Screen Shot 2020-06-10 at 5 49 18 PM

Having trouble on VS but still seems to be a problem on VSCode

lewing commented 4 years ago

I can reproduce this on 3.2 it works correctly on 5

using System;
using System.IO;
using System.Threading.Tasks;

namespace Bmem
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var data = new byte [] {1,2,3,4};
            await CreateMemoStream (data);
        }   

        private static async Task<Stream> CreateMemoStream(byte[] bytes)
        {
            Stream s = new MemoryStream();
            await s.WriteAsync(bytes);
            s.Position = 0;
            await s.FlushAsync();
            //debug
            var br = new BinaryReader(s);
            var bytes2 = new byte[s.Length];
            br.Read(bytes2);
            Console.WriteLine(string.Join(',', bytes2));// here is "0,0,0,...,0"
            return s;
        }
    }
}
lewing commented 4 years ago

@baulig can you take a look at this?