Beispiel #1
0
        private async ValueTask <ResponseMessage> ProgressPipeline(
            HttpContext context,
            RequestDelegate next,
            RequestMessage requestMessage,
            ResponseMessage responseMessage,
            CancellationToken cancellationToken
            )
        {
            var requestFeature  = new RequestFeature(requestMessage);
            var responseFeature = new ResponseFeature(responseMessage);

            var requestLifetimeFeature = new HttpRequestLifetimeFeature
            {
                RequestAborted = cancellationToken
            };

            var serviceProviderFeature       = context.Features.Get <IServiceProvidersFeature>();
            var requestAuthenticationFeature = context.Features.Get <IHttpAuthenticationFeature>();
            var formFeature             = context.Features.Get <IFormFeature>();
            var responseTrailersFeature = context.Features.Get <IHttpResponseTrailersFeature>();

            //var httpResponseBodyFeature = context.Features.Get<IHttpResponseBodyFeature>();
            //var httpBodyControlFeature = context.Features.Get<IHttpResponseBodyPipeFeature>();
            //var httpResponseBodyFeature = context.Features.Get<IHttpResponseFeature>();

            GC.KeepAlive(context.Features);

            var features = new FeatureCollection();

            features.Set <IHttpRequestFeature>(new HttpRequestFeature());
            features.Set <IHttpResponseFeature>(new HttpResponseFeature());

//            features.Set<IHttpRequestFeature>(requestFeature);
//            features.Set<IHttpResponseFeature>(responseFeature);

            features.Set <IHttpRequestLifetimeFeature>(requestLifetimeFeature);
            features.Set <IHttpAuthenticationFeature>(requestAuthenticationFeature);
            features.Set <IServiceProvidersFeature>(serviceProviderFeature);
            features.Set <IFormFeature>(formFeature);
            features.Set <IHttpResponseTrailersFeature>(responseTrailersFeature);
            //features.Set<IHttpBodyControlFeature>(httpBodyControlFeature);
            //features.Set<IHttpResponseBodyFeature>(httpResponseBodyFeature);
            //features.Set<IHttpResponseBodyPipeFeature>(null);

            var ctx = new DefaultHttpContext(features);

            await MessageConverter.CopyRequestMessageToTarget(ctx.Request, requestMessage, cancellationToken);

            IServiceProvider serviceProvider;

            if (Configuration.InProcessScopeMode != RedTransportInProcessScopeMode.UseRootScope)
            {
                //creating a new scope for IServiceProvider.

                var factory = serviceProviderFeature.RequestServices.GetRequiredService <IServiceScopeFactory>();

                serviceProvider = factory.CreateScope().ServiceProvider;

                serviceProviderFeature = new ServiceProvidersFeature
                {
                    RequestServices = serviceProvider
                };
                features.Set <IServiceProvidersFeature>(serviceProviderFeature);
            }
            else
            {
                serviceProvider = serviceProviderFeature.RequestServices;
            }

            var memStream = new MemoryStream();

            ctx.Response.Body = memStream;

            //GC.KeepAlive(ctx.Response.Body);

            ctx.Response.RegisterForDispose(memStream);

            await next(ctx);

            return(await MessageConverter.FromResponseAsync(ctx.Response, cancellationToken));
        }