public async Task WriteAsync(object value) { if (!(_readyTaskCompletionSource.Task.IsCompleted && !(_readyTaskCompletionSource.Task.IsCanceled || _readyTaskCompletionSource.Task.IsFaulted))) { await _readyTaskCompletionSource.Task; } await _ndjsonTextWriter.WriteAsync(value); }
/// <summary> /// Asynchronously writes the specified value to the stream. /// </summary> /// <param name="value">The value.</param> /// <param name="cancellationToken">A token that may be used to cancel the write operation.</param> /// <returns>A <see cref="Task"/> that represents the asynchronous write operation.</returns> public async Task WriteAsync(object value, CancellationToken cancellationToken) { if (!_readyTaskCompletionSource.Task.IsCompletedSuccessfully) { await _readyTaskCompletionSource.Task; } await _ndjsonTextWriter.WriteAsync(value, cancellationToken); }
/// <summary> /// Executes the result operation of the action method asynchronously. This method is called by MVC to process the result of an action method. /// </summary> /// <param name="context">The <see cref="ActionContext"/> in which the result is executed. The context information includes information about the action that was executed and request information.</param> /// <returns>A <see cref="Task"/> that represents the asynchronous execute operation.</returns> public override async Task ExecuteResultAsync(ActionContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } INdjsonWriterFactory ndjsonTextWriterFactory = context.HttpContext.RequestServices.GetRequiredService <INdjsonWriterFactory>(); using (INdjsonWriter ndjsonTextWriter = ndjsonTextWriterFactory.CreateWriter(context, this)) { await foreach (object value in _values) { await ndjsonTextWriter.WriteAsync(value); } } }
/// <summary> /// Executes the result operation of the action method asynchronously. This method is called by MVC to process the result of an action method. /// </summary> /// <param name="context">The <see cref="ActionContext"/> in which the result is executed. The context information includes information about the action that was executed and request information.</param> /// <returns>A <see cref="Task"/> that represents the asynchronous execute operation.</returns> public override async Task ExecuteResultAsync(ActionContext context) { if (context is null) { throw new ArgumentNullException(nameof(context)); } INdjsonWriterFactory ndjsonTextWriterFactory = context.HttpContext.RequestServices.GetRequiredService <INdjsonWriterFactory>(); using INdjsonWriter <T> ndjsonTextWriter = ndjsonTextWriterFactory.CreateWriter <T>(context, this); try { await foreach (T value in _values.WithCancellation(context.HttpContext.RequestAborted)) { await ndjsonTextWriter.WriteAsync(value, context.HttpContext.RequestAborted); } } catch (OperationCanceledException) when(context.HttpContext.RequestAborted.IsCancellationRequested) { } }