Exemplo n.º 1
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.º 2
0
        public async Task HandleCallAsync(HttpContext httpContext)
        {
            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);

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