You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Edit: For anyone seeing this later, my PR doesn't and wouldn't work. In order for an IAsyncEnumerable to be memory safe the top level signature needs to be IAsyncEnumerable<TResult> not Task<IAsyncEnumerable <TResult>>. An IAsyncEnumerable uses an implicit using block, but only after iteration begins.
To work around this issue, I've been creating separate 'streaming' endpoints that get generated in nswag as functions that just return 'Stream' in addition to the normal response of IList and then using this helper method to create a IAsyncEnumerable
private class CustomAsyncEnumerable<TData> : IAsyncEnumerable<TData>
{
private readonly Func<Task<Stream>> operation;
private readonly JsonSerializerOptions jsonSerializerOptions;
private bool didIterate;
public CustomAsyncEnumerable(
Func<Task<Stream>> operation,
JsonSerializerOptions jsonSerializerOptions)
{
this.operation = operation ?? throw new ArgumentNullException(nameof(operation));
this.jsonSerializerOptions = jsonSerializerOptions;
}
public async IAsyncEnumerator<TData> GetAsyncEnumerator(CancellationToken cancellation = default)
{
if (this.didIterate)
{
throw new NotSupportedException($"Can only iterate once.");
}
this.didIterate = true;
Stream stream;
try
{
stream = await this.operation().ConfigureAwait(false);
}
catch (Microsoft.Rest.HttpOperationException responseEx)
{
throw new InvalidOperationException($"Error querying: {responseEx.Response?.Content}", responseEx);
}
using (stream)
{
var rows = JsonSerializer.DeserializeAsyncEnumerable<TData>(stream, options: this.jsonSerializerOptions, cancellation);
await foreach (var row in rows)
{
yield return row;
}
}
}
}
The text was updated successfully, but these errors were encountered:
Dotnet 6 now easily supports streaming IAsyncEnumerable controller responses.
It would be nice to be able to consume the stream via a generated client using JsonSerializer.DeserializeAsyncEnumerable.
Possible PR here #3908?Edit: For anyone seeing this later, my PR doesn't and wouldn't work. In order for an IAsyncEnumerable to be memory safe the top level signature needs to be
IAsyncEnumerable<TResult>
notTask<IAsyncEnumerable <TResult>>
. An IAsyncEnumerable uses an implicit using block, but only after iteration begins.To work around this issue, I've been creating separate 'streaming' endpoints that get generated in nswag as functions that just return 'Stream' in addition to the normal response of IList and then using this helper method to create a IAsyncEnumerable
The text was updated successfully, but these errors were encountered: