Skip to content

.NET to JS Streaming Interop #34817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 6, 2021
Merged

.NET to JS Streaming Interop #34817

merged 16 commits into from
Aug 6, 2021

Conversation

TanayParikh
Copy link
Contributor

@TanayParikh TanayParikh commented Jul 28, 2021

  • Supports Blazor Server, WASM & WebView
  • Blazor Server leverages SignalR Streaming capabilities
  • WASM & WebView have a shared implementation in which .NET sends data chunks to JS for consumption via stream.

Usage:

  using var data = new System.IO.MemoryStream(new byte[100 * 1024]);
  using var streamRef = new DotNetStreamReference(stream: data, leaveOpen: false);
  await JS.InvokeVoidAsync("consumeStream", streamRef);
async function consumeStream(streamRef) {
    const data = await streamRef.arrayBuffer();    // ArrayBuffer
    // or
    const stream = await streamRef.stream();       // ReadableStream
}

Based on @SteveSandersonMS's proof of concept: #32848 (which also has a great writeup on how the pending stream logic works for Blazor Server).

  • TODO:
    • Unit Tests
    • E2E Tests

Fixes: #30289
API Review: #35009

@TanayParikh TanayParikh requested review from Pilchie and a team as code owners July 28, 2021 20:18
@TanayParikh TanayParikh added the area-blazor Includes: Blazor, Razor Components label Jul 28, 2021
@@ -20,6 +21,7 @@ internal class RemoteJSRuntime : JSRuntime
private readonly CircuitOptions _options;
private readonly ILogger<RemoteJSRuntime> _logger;
private CircuitClientProxy _clientProxy;
private readonly ConcurrentDictionary<long, DotNetStreamReference> _pendingDotNetToJSStreams = new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the concurrent dictionary here? Shouldn't this already be scoped to the circuit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cancellationToken.Register which is used to evict streams which aren't read from after the timeout, does not capture the Synchronization context.. Hence, without a ConcurrentDictionary we could run into potential issues when the cancellation token expires (and attempts to evict the stream) at the exact time we get a call into TryClaimPendingStreamForSending to evict the stream for consumption.

@javiercn
Copy link
Member

javiercn commented Aug 3, 2021

Code looks good, waiting for the E2E tests

@TanayParikh TanayParikh added the api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews label Aug 3, 2021
@ghost
Copy link

ghost commented Aug 3, 2021

Thank you for your API proposal. I'm removing the api-ready-for-review label. API Proposals should be submitted for review through Issues based on this template.

@ghost ghost removed the api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews label Aug 3, 2021
@TanayParikh TanayParikh linked an issue Aug 3, 2021 that may be closed by this pull request
@TanayParikh
Copy link
Contributor Author

This PR should be good to go now using the IAsyncEnumerable based approach to interface with SignalR streaming.

@javiercn
Copy link
Member

javiercn commented Aug 6, 2021

I might be missing something, but I don't see the E2E tests, can you point me to them?

Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.NET to JS Streaming Interop API Proposal Support Streaming Interop in Blazor
2 participants