Microsoft.AspNetCore.Buffering.BufferingWriteStream.FlushAsync C# (CSharp) Méthode

FlushAsync() public méthode

public FlushAsync ( CancellationToken cancellationToken ) : Task
cancellationToken System.Threading.CancellationToken
Résultat Task
        public override async Task FlushAsync(CancellationToken cancellationToken)
        {
            _isBuffering = false;
            if (_buffer.Length > 0)
            {
                _buffer.Seek(0, SeekOrigin.Begin);
                await _buffer.CopyToAsync(_innerStream, 1024 * 16, cancellationToken);
                _buffer.Seek(0, SeekOrigin.Begin);
                _buffer.SetLength(0);
            }
            await _innerStream.FlushAsync(cancellationToken);
        }

Usage Example

Exemple #1
0
        public async Task Invoke(HttpContext httpContext)
        {
            var originalResponseBody = httpContext.Response.Body;

            // no-op if buffering is already available.
            if (originalResponseBody.CanSeek)
            {
                await _next(httpContext);

                return;
            }

            var originalBufferingFeature = httpContext.Features.Get <IHttpBufferingFeature>();
            var originalSendFileFeature  = httpContext.Features.Get <IHttpSendFileFeature>();

            try
            {
                // Shim the response stream
                var bufferStream = new BufferingWriteStream(originalResponseBody);
                httpContext.Response.Body = bufferStream;
                httpContext.Features.Set <IHttpBufferingFeature>(new HttpBufferingFeature(bufferStream, originalBufferingFeature));
                if (originalSendFileFeature != null)
                {
                    httpContext.Features.Set <IHttpSendFileFeature>(new SendFileFeatureWrapper(originalSendFileFeature, bufferStream));
                }

                await _next(httpContext);

                // If we're still buffered, set the content-length header and flush the buffer.
                // Only if the content-length header is not already set, and some content was buffered.
                if (!httpContext.Response.HasStarted && bufferStream.CanSeek && bufferStream.Length > 0)
                {
                    if (!httpContext.Response.ContentLength.HasValue)
                    {
                        httpContext.Response.ContentLength = bufferStream.Length;
                    }
                    await bufferStream.FlushAsync();
                }
            }
            finally
            {
                // undo everything
                httpContext.Features.Set(originalBufferingFeature);
                httpContext.Features.Set(originalSendFileFeature);
                httpContext.Response.Body = originalResponseBody;
            }
        }
All Usage Examples Of Microsoft.AspNetCore.Buffering.BufferingWriteStream::FlushAsync