Пример #1
0
    protected override async Task HandleCallAsyncCore(HttpContext httpContext, JsonTranscodingServerCallContext serverCallContext)
    {
        var request = await JsonRequestHelpers.ReadMessage <TRequest>(serverCallContext, SerializerOptions);

        var response = await _invoker.Invoke(httpContext, serverCallContext, request);

        if (serverCallContext.Status.StatusCode != StatusCode.OK)
        {
            throw new RpcException(serverCallContext.Status);
        }

        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."));
        }

        if (response is HttpBody httpBody)
        {
            serverCallContext.EnsureResponseHeaders(httpBody.ContentType);
            await serverCallContext.HttpContext.Response.Body.WriteAsync(httpBody.Data.Memory);
        }
        else
        {
            serverCallContext.EnsureResponseHeaders();
            await JsonRequestHelpers.SendMessage(serverCallContext, SerializerOptions, response, CancellationToken.None);
        }
    }
Пример #2
0
        protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpApiServerCallContext serverCallContext)
        {
            // Decode request
            var request = await JsonRequestHelpers.ReadMessage <TRequest>(serverCallContext, SerializerOptions);

            var streamWriter = new HttpContextStreamWriter <TResponse>(serverCallContext, SerializerOptions);

            try
            {
                await _invoker.Invoke(httpContext, serverCallContext, request, streamWriter);
            }
            finally
            {
                streamWriter.Complete();
            }
        }
Пример #3
0
        protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpApiServerCallContext serverCallContext)
        {
            var request = await JsonRequestHelpers.ReadMessage <TRequest>(serverCallContext, SerializerOptions);

            var response = await _invoker.Invoke(httpContext, serverCallContext, request);

            if (serverCallContext.Status.StatusCode != StatusCode.OK)
            {
                throw new RpcException(serverCallContext.Status);
            }

            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."));
            }

            httpContext.Response.StatusCode  = StatusCodes.Status200OK;
            httpContext.Response.ContentType = MediaType.ReplaceEncoding("application/json", serverCallContext.RequestEncoding);

            await JsonRequestHelpers.SendMessage(serverCallContext, SerializerOptions, response);
        }