Пример #1
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();
        }
Пример #2
0
        public override async Task HandleCallAsync(HttpContext httpContext)
        {
            httpContext.Response.ContentType = "application/grpc";
            httpContext.Response.Headers.Append("grpc-encoding", "identity");

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

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

            TResponse response = null;

            try
            {
                serverCallContext.Initialize();

                service = activator.Create();

                response = await _invoker(
                    service,
                    new HttpContextStreamReader <TRequest>(httpContext, 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, "Cancelled"));
                }

                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();
        }
        public override async Task HandleCallAsync(HttpContext httpContext)
        {
            httpContext.Response.ContentType = "application/grpc";
            httpContext.Response.Headers.Append("grpc-encoding", "identity");

            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();
        }
        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();

                service = activator.Create();

                await _invoker(
                    service,
                    new HttpContextStreamReader <TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
                    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();
        }