コード例 #1
0
        /// <summary>
        /// Invoke the middleware.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context)
        {
            if (!_provider.CheckRequestAcceptsCompression(context))
            {
                await _next(context);

                return;
            }

            var originalBodyFeature        = context.Features.Get <IHttpResponseBodyFeature>();
            var originalCompressionFeature = context.Features.Get <IHttpsCompressionFeature>();

            var compressionBody = new ResponseCompressionBody(context, _provider, originalBodyFeature);

            context.Features.Set <IHttpResponseBodyFeature>(compressionBody);
            context.Features.Set <IHttpsCompressionFeature>(compressionBody);

            try
            {
                await _next(context);

                await compressionBody.FinishCompressionAsync();
            }
            finally
            {
                context.Features.Set(originalBodyFeature);
                context.Features.Set(originalCompressionFeature);
            }
        }
コード例 #2
0
        private async Task InvokeCore(HttpContext context)
        {
            var originalBodyFeature        = context.Features.Get <IHttpResponseBodyFeature>();
            var originalCompressionFeature = context.Features.Get <IHttpsCompressionFeature>();

            Debug.Assert(originalBodyFeature != null);

            var compressionBody = new ResponseCompressionBody(context, _provider, originalBodyFeature);

            context.Features.Set <IHttpResponseBodyFeature>(compressionBody);
            context.Features.Set <IHttpsCompressionFeature>(compressionBody);

            try
            {
                await _next(context);

                await compressionBody.FinishCompressionAsync();
            }
            finally
            {
                context.Features.Set(originalBodyFeature);
                context.Features.Set(originalCompressionFeature);
            }
        }