Ejemplo n.º 1
0
        public Task HandleCallAsync(HttpContext httpContext)
        {
            if (GrpcProtocolHelpers.IsInvalidContentType(httpContext, out var error))
            {
                GrpcProtocolHelpers.SendHttpError(httpContext.Response, StatusCodes.Status415UnsupportedMediaType, StatusCode.Internal, error !);
                return(Task.CompletedTask);
            }

            var serverCallContext = new HttpContextServerCallContext(httpContext, ServiceOptions, Logger);

            httpContext.Features.Set <IServerCallContextFeature>(serverCallContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            try
            {
                serverCallContext.Initialize();

                var handleCallTask = HandleCallAsyncCore(httpContext, serverCallContext);

                if (handleCallTask.IsCompletedSuccessfully)
                {
                    return(serverCallContext.EndCallAsync());
                }
                else
                {
                    return(AwaitHandleCall(serverCallContext, Method, handleCallTask));
                }
            }
            catch (Exception ex)
            {
                return(serverCallContext.ProcessHandlerErrorAsync(ex, Method.Name));
            }
Ejemplo n.º 2
0
        protected override async Task HandleCallAsyncCore(HttpContext httpContext)
        {
            var serverCallContext = CreateServerCallContext(httpContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            try
            {
                serverCallContext.Initialize();

                var requestPayload = await httpContext.Request.BodyReader.ReadSingleMessageAsync(serverCallContext);

                var request = Method.RequestMarshaller.Deserializer(requestPayload);

                TResponse?response = null;

                if (_pipelineInvoker == null)
                {
                    var      activator = httpContext.RequestServices.GetRequiredService <IGrpcServiceActivator <TService> >();
                    TService?service   = null;
                    try
                    {
                        service  = activator.Create();
                        response = await _invoker(service, request, serverCallContext);
                    }
                    finally
                    {
                        if (service != null)
                        {
                            activator.Release(service);
                        }
                    }
                }
                else
                {
                    response = await _pipelineInvoker(request, serverCallContext);
                }

                if (response == null)
                {
                    // This is consistent with Grpc.Core when a null value is returned
                    throw new RpcException(new Status(StatusCode.Cancelled, "No message returned from method."));
                }

                var responseBodyWriter = httpContext.Response.BodyWriter;
                await responseBodyWriter.WriteMessageAsync(response, serverCallContext, Method.ResponseMarshaller.Serializer);

                await serverCallContext.EndCallAsync();
            }
            catch (Exception ex)
            {
                serverCallContext.ProcessHandlerError(ex, Method.Name);
            }
            finally
            {
                serverCallContext.Dispose();
            }
        }
Ejemplo n.º 3
0
        public Task HandleCallAsync(HttpContext httpContext)
        {
            if (GrpcProtocolHelpers.IsInvalidContentType(httpContext, out var error))
            {
                // This might be a CORS preflight request and CORS middleware hasn't been configured
                if (HttpMethods.IsOptions(httpContext.Request.Method))
                {
                    GrpcServerLog.UnhandledCorsPreflightRequest(Logger);

                    GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status405MethodNotAllowed, StatusCode.Internal, "Unhandled CORS preflight request received. CORS may not be configured correctly in the application.");
                    httpContext.Response.Headers[HeaderNames.Allow] = HttpMethods.Post;
                    return(Task.CompletedTask);
                }
                else
                {
                    GrpcServerLog.UnsupportedRequestContentType(Logger, httpContext.Request.ContentType);

                    GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status415UnsupportedMediaType, StatusCode.Internal, error);
                    return(Task.CompletedTask);
                }
            }
            if (httpContext.Request.Protocol != GrpcProtocolConstants.Http2Protocol &&
                httpContext.Request.Protocol != GrpcProtocolConstants.Http20Protocol)
            {
                GrpcServerLog.UnsupportedRequestProtocol(Logger, httpContext.Request.Protocol);

                var protocolError = $"Request protocol '{httpContext.Request.Protocol}' is not supported.";
                GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status426UpgradeRequired, StatusCode.Internal, protocolError);
                httpContext.Response.Headers[HeaderNames.Upgrade] = GrpcProtocolConstants.Http2Protocol;
                return(Task.CompletedTask);
            }

            var serverCallContext = new HttpContextServerCallContext(httpContext, MethodInvoker.Options, typeof(TRequest), typeof(TResponse), Logger);

            httpContext.Features.Set <IServerCallContextFeature>(serverCallContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            try
            {
                serverCallContext.Initialize();

                var handleCallTask = HandleCallAsyncCore(httpContext, serverCallContext);

                if (handleCallTask.IsCompletedSuccessfully)
                {
                    return(serverCallContext.EndCallAsync());
                }
                else
                {
                    return(AwaitHandleCall(serverCallContext, MethodInvoker.Method, handleCallTask));
                }
            }
            catch (Exception ex)
            {
                return(serverCallContext.ProcessHandlerErrorAsync(ex, MethodInvoker.Method.Name));
            }
Ejemplo n.º 4
0
        protected override async Task HandleCallAsyncCore(HttpContext httpContext)
        {
            var serverCallContext = CreateServerCallContext(httpContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);


            try
            {
                serverCallContext.Initialize();

                // Decode request
                var requestPayload = await httpContext.Request.BodyReader.ReadSingleMessageAsync(serverCallContext);

                var request = Method.RequestMarshaller.Deserializer(requestPayload);

                if (_pipelineInvoker == null)
                {
                    var      activator = httpContext.RequestServices.GetRequiredService <IGrpcServiceActivator <TService> >();
                    TService?service   = null;
                    try
                    {
                        service = activator.Create();
                        await _invoker(
                            service,
                            request,
                            new HttpContextStreamWriter <TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
                            serverCallContext);
                    }
                    finally
                    {
                        if (service != null)
                        {
                            activator.Release(service);
                        }
                    }
                }
                else
                {
                    await _pipelineInvoker(
                        request,
                        new HttpContextStreamWriter <TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
                        serverCallContext);
                }

                await serverCallContext.EndCallAsync();
            }
            catch (Exception ex)
            {
                serverCallContext.ProcessHandlerError(ex, Method.Name);
            }
            finally
            {
                serverCallContext.Dispose();
            }
        }
Ejemplo n.º 5
0
        protected override async Task HandleCallAsyncCore(HttpContext httpContext)
        {
            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            var serverCallContext = new HttpContextServerCallContext(httpContext, ServiceOptions, Logger);

            var      activator = httpContext.RequestServices.GetRequiredService <IGrpcServiceActivator <TService> >();
            TService service   = null;

            TResponse response = null;

            try
            {
                serverCallContext.Initialize();

                var requestPayload = await httpContext.Request.BodyPipe.ReadSingleMessageAsync(serverCallContext);

                var request = Method.RequestMarshaller.Deserializer(requestPayload);

                service = activator.Create();

                response = await _invoker(
                    service,
                    request,
                    serverCallContext);

                if (response == null)
                {
                    // This is consistent with Grpc.Core when a null value is returned
                    throw new RpcException(new Status(StatusCode.Cancelled, "No message returned from method."));
                }

                var responseBodyPipe = httpContext.Response.BodyPipe;
                await responseBodyPipe.WriteMessageAsync(response, serverCallContext, Method.ResponseMarshaller.Serializer);
            }
            catch (Exception ex)
            {
                serverCallContext.ProcessHandlerError(ex, Method.Name);
            }
            finally
            {
                serverCallContext.Dispose();
                if (service != null)
                {
                    activator.Release(service);
                }
            }

            httpContext.Response.ConsolidateTrailers(serverCallContext);

            // Flush any buffered content
            await httpContext.Response.BodyPipe.FlushAsync();
        }
        protected override async Task HandleCallAsyncCore(HttpContext httpContext)
        {
            // Disable request body data rate for client streaming
            DisableMinRequestBodyDataRate(httpContext);

            var serverCallContext = CreateServerCallContext(httpContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            try
            {
                serverCallContext.Initialize();

                if (_pipelineInvoker == null)
                {
                    var      activator = httpContext.RequestServices.GetRequiredService <IGrpcServiceActivator <TService> >();
                    TService?service   = null;
                    try
                    {
                        service = activator.Create();
                        await _invoker(
                            service,
                            new HttpContextStreamReader <TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
                            new HttpContextStreamWriter <TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
                            serverCallContext);
                    }
                    finally
                    {
                        if (service != null)
                        {
                            activator.Release(service);
                        }
                    }
                }
                else
                {
                    await _pipelineInvoker(
                        new HttpContextStreamReader <TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
                        new HttpContextStreamWriter <TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
                        serverCallContext);
                }

                await serverCallContext.EndCallAsync();
            }
            catch (Exception ex)
            {
                serverCallContext.ProcessHandlerError(ex, Method.Name);
            }
            finally
            {
                serverCallContext.Dispose();
            }
        }
Ejemplo n.º 7
0
        public Task HandleCallAsync(HttpContext httpContext)
        {
            if (GrpcProtocolHelpers.IsInvalidContentType(httpContext, out var error))
            {
                Log.UnsupportedRequestContentType(Logger, httpContext.Request.ContentType);

                GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status415UnsupportedMediaType, StatusCode.Internal, error);
                return(Task.CompletedTask);
            }
            if (httpContext.Request.Protocol != GrpcProtocolConstants.Http2Protocol &&
                httpContext.Request.Protocol != GrpcProtocolConstants.Http20Protocol)
            {
                Log.UnsupportedRequestProtocol(Logger, httpContext.Request.Protocol);

                var protocolError = $"Request protocol '{httpContext.Request.Protocol}' is not supported.";
                GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status426UpgradeRequired, StatusCode.Internal, protocolError);
                httpContext.Response.Headers[HeaderNames.Upgrade] = GrpcProtocolConstants.Http2Protocol;
                return(Task.CompletedTask);
            }

            var serverCallContext = new HttpContextServerCallContext(httpContext, ServiceOptions, Logger);

            httpContext.Features.Set <IServerCallContextFeature>(serverCallContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            try
            {
                serverCallContext.Initialize();

                var handleCallTask = HandleCallAsyncCore(httpContext, serverCallContext);

                if (handleCallTask.IsCompletedSuccessfully)
                {
                    return(serverCallContext.EndCallAsync());
                }
                else
                {
                    return(AwaitHandleCall(serverCallContext, Method, handleCallTask));
                }
            }
            catch (Exception ex)
            {
                return(serverCallContext.ProcessHandlerErrorAsync(ex, Method.Name));
            }
        protected override async Task HandleCallAsyncCore(HttpContext httpContext)
        {
            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            var serverCallContext = new HttpContextServerCallContext(httpContext, ServiceOptions, Logger);

            var      activator = httpContext.RequestServices.GetRequiredService <IGrpcServiceActivator <TService> >();
            TService service   = null;

            try
            {
                serverCallContext.Initialize();

                // Decode request
                var requestPayload = await httpContext.Request.BodyPipe.ReadSingleMessageAsync(serverCallContext);

                var request = Method.RequestMarshaller.Deserializer(requestPayload);

                service = activator.Create();

                await _invoker(
                    service,
                    request,
                    new HttpContextStreamWriter <TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
                    serverCallContext);
            }
            catch (Exception ex)
            {
                serverCallContext.ProcessHandlerError(ex, Method.Name);
            }
            finally
            {
                serverCallContext.Dispose();
                if (service != null)
                {
                    activator.Release(service);
                }
            }

            httpContext.Response.ConsolidateTrailers(serverCallContext);

            // Flush any buffered content
            await httpContext.Response.BodyPipe.FlushAsync();
        }
        public Task HandleCallAsync(HttpContext httpContext)
        {
            if (GrpcProtocolHelpers.IsInvalidContentType(httpContext, out var error))
            {
                return(ProcessInvalidContentTypeRequest(httpContext, error));
            }

            if (!GrpcProtocolConstants.IsHttp2(httpContext.Request.Protocol)
#if NET6_0_OR_GREATER
                && !GrpcProtocolConstants.IsHttp3(httpContext.Request.Protocol)
#endif
                )
            {
                return(ProcessNonHttp2Request(httpContext));
            }

            var serverCallContext = new HttpContextServerCallContext(httpContext, MethodInvoker.Options, typeof(TRequest), typeof(TResponse), Logger);
            httpContext.Features.Set <IServerCallContextFeature>(serverCallContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            try
            {
                serverCallContext.Initialize();

                var handleCallTask = HandleCallAsyncCore(httpContext, serverCallContext);

                if (handleCallTask.IsCompletedSuccessfully)
                {
                    return(serverCallContext.EndCallAsync());
                }
                else
                {
                    return(AwaitHandleCall(serverCallContext, MethodInvoker.Method, handleCallTask));
                }
            }
            catch (Exception ex)
            {
                return(serverCallContext.ProcessHandlerErrorAsync(ex, MethodInvoker.Method.Name));
            }
Ejemplo n.º 10
0
        protected override async Task HandleCallAsyncCore(HttpContext httpContext)
        {
            var serverCallContext = CreateServerCallContext(httpContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            var      activator = httpContext.RequestServices.GetRequiredService <IGrpcServiceActivator <TService> >();
            TService service   = null;

            try
            {
                serverCallContext.Initialize();

                // Decode request
                var requestPayload = await httpContext.Request.BodyReader.ReadSingleMessageAsync(serverCallContext);

                var request = Method.RequestMarshaller.Deserializer(requestPayload);


                if (ServiceOptions.Interceptors.IsEmpty)
                {
                    try
                    {
                        service = activator.Create();
                        await _invoker(
                            service,
                            request,
                            new HttpContextStreamWriter <TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
                            serverCallContext);
                    }
                    finally
                    {
                        if (service != null)
                        {
                            activator.Release(service);
                        }
                    }
                }
                else
                {
                    ServerStreamingServerMethod <TRequest, TResponse> resolvedInvoker = async(request, responseStream, resolvedContext) =>
                    {
                        try
                        {
                            service = activator.Create();
                            await _invoker(
                                service,
                                request,
                                responseStream,
                                resolvedContext);
                        }
                        finally
                        {
                            if (service != null)
                            {
                                activator.Release(service);
                            }
                        }
                    };

                    // The list is reversed during construction so the first interceptor is built last and invoked first
                    for (var i = ServiceOptions.Interceptors.Count - 1; i >= 0; i--)
                    {
                        resolvedInvoker = BuildInvoker(ServiceOptions.Interceptors[i], resolvedInvoker);
                    }

                    await resolvedInvoker(
                        request,
                        new HttpContextStreamWriter <TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
                        serverCallContext);
                }

                await serverCallContext.EndCallAsync();
            }
            catch (Exception ex)
            {
                serverCallContext.ProcessHandlerError(ex, Method.Name);
            }
            finally
            {
                serverCallContext.Dispose();
                if (service != null)
                {
                    activator.Release(service);
                }
            }
        }
        protected override async Task HandleCallAsyncCore(HttpContext httpContext)
        {
            var serverCallContext = CreateServerCallContext(httpContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            var       activator = httpContext.RequestServices.GetRequiredService <IGrpcServiceActivator <TService> >();
            TService  service   = null;
            TResponse response  = null;

            try
            {
                serverCallContext.Initialize();

                if (ServiceOptions.Interceptors.IsEmpty)
                {
                    try
                    {
                        service  = activator.Create();
                        response = await _invoker(
                            service,
                            new HttpContextStreamReader <TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
                            serverCallContext);
                    }
                    finally
                    {
                        if (service != null)
                        {
                            activator.Release(service);
                        }
                    }
                }
                else
                {
                    ClientStreamingServerMethod <TRequest, TResponse> resolvedInvoker = async(resolvedRequestStream, resolvedContext) =>
                    {
                        try
                        {
                            service = activator.Create();
                            return(await _invoker(
                                       service,
                                       resolvedRequestStream,
                                       resolvedContext));
                        }
                        finally
                        {
                            if (service != null)
                            {
                                activator.Release(service);
                            }
                        }
                    };

                    // The list is reversed during construction so the first interceptor is built last and invoked first
                    for (var i = ServiceOptions.Interceptors.Count - 1; i >= 0; i--)
                    {
                        resolvedInvoker = BuildInvoker(ServiceOptions.Interceptors[i], resolvedInvoker);
                    }

                    response = await resolvedInvoker(
                        new HttpContextStreamReader <TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
                        serverCallContext);
                }

                if (response == null)
                {
                    // This is consistent with Grpc.Core when a null value is returned
                    throw new RpcException(new Status(StatusCode.Cancelled, "No message returned from method."));
                }

                var responseBodyWriter = httpContext.Response.BodyWriter;
                await responseBodyWriter.WriteMessageAsync(response, serverCallContext, Method.ResponseMarshaller.Serializer);

                await serverCallContext.EndCallAsync();
            }
            catch (Exception ex)
            {
                serverCallContext.ProcessHandlerError(ex, Method.Name);
            }
            finally
            {
                serverCallContext.Dispose();
            }
        }