0

I need to stream a file from ASP.NET Core to Blazor WASM using gRPC.

I would like to use the new class DotNetStreamReference added in net6 to allow stream directly to disk using JSInterop (see https://learn.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0).

Now, the problem is: gRPC method stream data returning a IAsyncEnumerable<T>, but the DotNetStreamReference class accept only a Stream object as input.

There is a way to convert IAsyncEnumerable<byte[]> to a Stream or eventually return a Stream from a gRPC call?

Thanks

Claudio
  • 133
  • 2
  • 11
  • I don't think there's a way to do this without modifying Grpc's code, but you might be lucky with some low-level hackery with custom serializers and deserializers https://github.com/grpc/grpc/blob/143d1a7e2cb188f6de9c37e39fe04576197b41cd/src/csharp/Grpc.Core.Api/Marshaller.cs#L121 and custom grpc method handlers. Overall, this is experts-only area and I'd recommend against doing that since the performance improvements aren't going to be worth the extra complexity (unless you've done extensive analysis before and it's THE bottleneck of your app). – Jan Tattermusch Jan 06 '22 at 12:25

1 Answers1

0

I'm not sure about the gRPC part, but for the 'convert' question, definitely:

var streamContent = new byte[] {};
await foreach(var chunk in your_iasyncenumerable)
{
    streamContent.Concat(chunk);
} 
Ergis
  • 1,105
  • 1
  • 7
  • 18
  • loop and create a byte array is not my goal, i need to stream the file to disk to preserve the memory allocation. – Claudio Nov 23 '21 at 17:42
  • You mean you want to stream the file to a `FileStream` rather than a `MemoryStream` – Ergis Nov 23 '21 at 18:19