Skip to content

Sep does not support async only streams (such as IBrowserFile) #27

@Shane88

Description

@Shane88

When trying to use Sep.Reader().From(stream) with a stream returned from IBrowserFile.OpenReadStream in the context of a user's browser in a .NET Blazor WASM application, it fails because Synchronous reads are not supported. To my understanding there is no other methods that can be used for Sep to use an asynchronous read from the supplied stream.

The stack trace will look like the following inside Blazor WASM:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Synchronous reads are not supported.
System.NotSupportedException: Synchronous reads are not supported.
   at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.StreamReader.ReadBuffer(Span`1 userBuffer, Boolean& readToUserBuffer)
   at System.IO.StreamReader.ReadSpan(Span`1 buffer)
   at System.IO.StreamReader.Read(Span`1 buffer)
   at nietras.SeparatedValues.SepReader.CheckCharsAvailableDataMaybeRead(Int32 paddingLength)
   at nietras.SeparatedValues.SepReader.EnsureInitializeAndReadData(Boolean endOfFile)
   at nietras.SeparatedValues.SepReader.MoveNext()
   at nietras.SeparatedValues.SepReader..ctor(Info info, SepReaderOptions options, TextReader reader)
   at nietras.SeparatedValues.SepReaderExtensions.FromWithInfo(Info info, SepReaderOptions options, TextReader reader)
   at nietras.SeparatedValues.SepReaderExtensions.From(SepReaderOptions options, Stream stream)

Note that BrowserFileStream belongs to AspNetCore.Components so I suspect this might also happen other frameworks like MVC but I haven't tested. The msdoc link for the method returning the async only stream is here (IBrowserFile.OpenReadStream(Int64, CancellationToken) Method)

Having had a good look over Seps docs I understand that async stream compatibility might be tricky and out of scope for the intended use of Sep. If that is the conclusion, it would be appreciated to update Sep docs to mention that.

Thinking about what async support might look like with Sep, I thought of the following.

// SepAsyncReader implements the IAsyncEnumerable pattern. 
using SepAsyncReader reader = Sep.Reader().FromAsync(stream);

await foreach (SepReader.Row row in reader)
{
   // Normal synchronous indexers at this point. 
   string myString = row["Column1"].ToString();
}

Cheers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions