Exemplo n.º 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);
        }
    }
        protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
        {
            var request = await httpContext.Request.BodyReader.ReadSingleMessageAsync <TRequest>(serverCallContext, MethodInvoker.Method.RequestMarshaller.ContextualDeserializer);

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

            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 (serverCallContext.DeadlineManager != null && serverCallContext.DeadlineManager.CancellationToken.IsCancellationRequested)
            {
                // The cancellation token has been raised. Ensure that any DeadlineManager tasks have
                // been completed before continuing.
                await serverCallContext.DeadlineManager.CancellationProcessedTask;

                // There is no point trying to write to the response because it has been finished.
                if (serverCallContext.DeadlineManager.CallComplete)
                {
                    return;
                }
            }

            var responseBodyWriter = httpContext.Response.BodyWriter;
            await responseBodyWriter.WriteMessageAsync(response, serverCallContext, MethodInvoker.Method.ResponseMarshaller.ContextualSerializer, canFlush : false);
        }
Exemplo n.º 3
0
        public async Task Invoke_AwaitedSuccess_ReleaseCalled()
        {
            // Arrange
            var methodTcs        = new TaskCompletionSource <TestMessage>(TaskCreationOptions.RunContinuationsAsynchronously);
            var methodResult     = new TestMessage();
            var serviceActivator = new TestGrpcServiceActivator <TestService>();
            var invoker          = new UnaryServerMethodInvoker <TestService, TestMessage, TestMessage>(
                (service, reader, context) => methodTcs.Task,
                new Method <TestMessage, TestMessage>(MethodType.Unary, "test", "test", _marshaller, _marshaller),
                HttpContextServerCallContextHelper.CreateMethodOptions(),
                serviceActivator);
            var httpContext = HttpContextHelpers.CreateContext();

            // Act
            var task = invoker.Invoke(httpContext, HttpContextServerCallContextHelper.CreateServerCallContext(), new TestMessage());

            Assert.False(task.IsCompleted);

            methodTcs.SetResult(methodResult);
            var awaitedResult = await task;

            // Assert
            Assert.AreEqual(methodResult, awaitedResult);
            Assert.True(serviceActivator.Released);
        }
        public async Task HandleCallAsync(HttpContext httpContext)
        {
            //  var requestMessage = await CreateMessage(httpContext.Request);

            var selectedEncoding = ResponseEncoding.SelectCharacterEncoding(httpContext.Request);

            var(requestMessage, requestStatusCode, errorMessage) = await CreateMessage(httpContext.Request);

            if (requestMessage == null || requestStatusCode != StatusCode.OK)
            {
                await SendErrorResponse(httpContext.Response, selectedEncoding, errorMessage ?? string.Empty, requestStatusCode);

                return;
            }
            var serverCallContext = new HttpApiServerCallContext(httpContext, _unaryMethodInvoker.Method.FullName);

            //var selectedEncoding = ResponseEncoding.SelectCharacterEncoding(httpContext.Request);

            TResponse responseMessage;

            try
            {
                responseMessage = await _unaryMethodInvoker.Invoke(httpContext, serverCallContext, (TRequest)requestMessage);
            }
            catch (Exception ex)
            {
                StatusCode statusCode;
                string     message;

                if (ex is RpcException rpcException)
                {
                    message    = rpcException.Message;
                    statusCode = rpcException.StatusCode;
                }
                else
                {
                    // TODO - Add option for detailed error messages
                    message    = "Exception was thrown by handler.";
                    statusCode = StatusCode.Unknown;
                }

                await SendErrorResponse(httpContext.Response, selectedEncoding, message, statusCode);

                return;
            }

            if (serverCallContext.Status.StatusCode != StatusCode.OK)
            {
                await SendErrorResponse(httpContext.Response, selectedEncoding, serverCallContext.Status.ToString(),
                                        serverCallContext.Status.StatusCode);

                return;
            }

            await SendResponse(httpContext.Response, selectedEncoding, responseMessage);
        }
        public async Task HandleCallAsync(HttpContext httpContext)
        {
            var requestMessage = await CreateMessage(httpContext);

            var serverCallContext = new HttpApiServerCallContext();

            var response = await _unaryMethodInvoker.Invoke(httpContext, serverCallContext, (TRequest)requestMessage);

            await SendResponse(httpContext, response);
        }
Exemplo n.º 6
0
        public async Task HandleCallAsync(HttpContext httpContext)
        {
            var requestMessage = await CreateMessage(httpContext.Request);

            var serverCallContext = new HttpApiServerCallContext();

            var responseMessage = await _unaryMethodInvoker.Invoke(httpContext, serverCallContext, (TRequest)requestMessage);

            var selectedEncoding = ResponseEncoding.SelectCharacterEncoding(httpContext.Request);

            await SendResponse(httpContext.Response, selectedEncoding, responseMessage);
        }
Exemplo n.º 7
0
        protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
        {
            var request = await httpContext.Request.BodyReader.ReadSingleMessageAsync <TRequest>(serverCallContext, MethodInvoker.Method.RequestMarshaller.ContextualDeserializer);

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

            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, MethodInvoker.Method.ResponseMarshaller.ContextualSerializer, canFlush : false);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
        {
            var request = await httpContext.Request.BodyReader.ReadSingleMessageAsync <TRequest>(serverCallContext, MethodInvoker.Method.RequestMarshaller.ContextualDeserializer);

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

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

            // Check if deadline exceeded while method was invoked. If it has then skip trying to write
            // the response message because it will always fail.
            // Note that the call is still going so the deadline could still be exceeded after this point.
            if (serverCallContext.DeadlineManager?.IsDeadlineExceededStarted ?? false)
            {
                return;
            }

            var responseBodyWriter = httpContext.Response.BodyWriter;
            await responseBodyWriter.WriteMessageAsync(response, serverCallContext, MethodInvoker.Method.ResponseMarshaller.ContextualSerializer, canFlush : false);
        }
Exemplo n.º 10
0
        public async Task Invoke_SuccessAwaitedRelease_ReleaseCalled()
        {
            // Arrange
            var releaseTcs       = new TaskCompletionSource <object?>(TaskCreationOptions.RunContinuationsAsynchronously);
            var serviceActivator = new TcsGrpcServiceActivator <TestService>(releaseTcs);
            var invoker          = new UnaryServerMethodInvoker <TestService, TestMessage, TestMessage>(
                (service, reader, context) => Task.FromResult(new TestMessage()),
                new Method <TestMessage, TestMessage>(MethodType.Unary, "test", "test", _marshaller, _marshaller),
                HttpContextServerCallContextHelper.CreateMethodOptions(),
                serviceActivator);
            var httpContext = HttpContextHelpers.CreateContext();

            // Act
            var task = invoker.Invoke(httpContext, HttpContextServerCallContextHelper.CreateServerCallContext(), new TestMessage());

            Assert.False(task.IsCompleted);

            releaseTcs.SetResult(null);
            await task;

            // Assert
            Assert.True(serviceActivator.Released);
        }