Tewr / BlazorFileReader

Library for creating read-only file streams from file input elements or drop targets in Blazor.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unexpected behavior for await call in while header when compiling in release mode / with flag optimize=true

erikthysell opened this issue · comments

Hi, first, thanks for a nice blazor plugin.
Using Blazor wasm app (NET standard 2.1) and the BlazorFileReader in the following code snippet (shortened, in my app it is divided into a .razor file and a .razor.cs file but shortened and put together here for the sake of simplicity):

@inject Blazor.FileReader.IFileReaderService fileReaderService
@using Blazor.FileReader;
    <div class="ml-2">
        <input type="file" @ref="InputElement" @onchange="FilesSelected" multiple accept=".txt,.csv" />
    </div>

@code {
        ElementReference InputElement;
        [Parameter] public List<string> FileContent { get; set; }
        async Task FilesSelected()
        {
            foreach (var file in await fileReaderService.CreateReference(InputElement).EnumerateFilesAsync())
            {
                using (Stream stream = await file.OpenReadAsync())
                {
                    FileContent = await ReadLinesAsync(stream, Encoding.UTF8);
                }
            }
        }
        public async Task<List<string>> ReadLinesAsync(Stream stream, Encoding encoding)
        {
            using (var reader = new StreamReader(stream, encoding))
            {
                string line;
                var result = new List<string>();
                while ((line = await reader.ReadLineAsync()) != null)
                {
                    result.Add(line);
                }
                return result;
            }
        }
}

Running this code locally (Ctrl+F5 in visual studio 2019 16.6 preview) reads any text file just fine. But when uploading this to Azure, it reads the correct number of lines but each line is empty. (as in string.Empty).
I do not know if this is a problem with BlazorFileReader or with blazor/azure....(?)

EDIT: Realizing it is probably not in the BlazorFileReader I will try to ask on SO...
EDIT2: I realized it probably has to do with StreamReader.ReadLineAsync() in "release" mode.

commented

hello!

I never got around to/managed reproduce this when I last heard about it, I think its a blazor bug, has something to do with the linker.
Try the solution in #97 in the meantime and see if that works for you. Seems like the exact same problem.

commented

I can reproduce this consistently. The workaround in #97 is still working.
I'm working on a minimalistic repro project to submit a bug to the blazor team. Seems like a really tricky bug. I'm no longer sure it is related to the linker. The workaround can be implemented side-by side, even in the same class, and there is no problem.

I'm more thinking the Optimization step in release mode. Perhaps something is wrong the async invocation and the c# generated from the .razor file.

commented

Confirmed, it's the optimizer. Problem is still present with linker turned off. Problem is no longer present when optimizer is turn off:

  <PropertyGroup>
    <Optimize>false</Optimize>
  </PropertyGroup>
commented

I have a full minimal repro example. This is definitely a framework bug in the optimizer, not related to this library.

Thanks a lot. The workaround is indeed working.

commented

This has been corrected in runtime but is not yet released, I'll keep this open until then

commented

Fixed in Blazor 5rc1