Exemple #1
0
        public async Task Invoke(HttpContext httpContext)
        {
            // serializer may be mutated in JsonRpcRequest.CreateAsync, so we need new instance for every request
            JsonSerializer serializer = JsonSerializer.Create(_options.JsonSerializerSettings);

            try
            {
                using (var scope = _rpcServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
                {
                    var requestServices = new CompositeServiceProvider(
                        httpContext.RequestServices,
                        scope.ServiceProvider
                        );

                    JsonRpcRequest request;
                    try
                    {
                        request = await JsonRpcRequest.CreateAsync(httpContext, serializer);
                    }
                    catch (Exception ex)
                    {
                        // todo: log
                        if (!httpContext.Response.HasStarted && !httpContext.RequestAborted.IsCancellationRequested)
                        {
                            await SendResponse(httpContext, new JsonRpcResponse(null, JsonRpcError.PARSE_ERROR, errorData : ex.ToString()), serializer);
                        }
                        return;
                    }

                    var rpcContext = new JsonRpcContext(requestServices, _server, request);

                    await _server.ProcessAsync(rpcContext);

                    if (rpcContext.Request.IsNotification)
                    {
                        httpContext.Response.StatusCode = 204;
                    }
                    else
                    {
                        await SendResponse(httpContext, rpcContext.Response, serializer);
                    }
                }
            }
            catch (Exception ex)
            {
                // todo: log
                if (!httpContext.Response.HasStarted && !httpContext.RequestAborted.IsCancellationRequested)
                {
                    await SendResponse(httpContext, new JsonRpcResponse(null, JsonRpcError.INTERNAL_ERROR, errorData : ex.ToString()), serializer);
                }
            }
        }
Exemple #2
0
        public async Task Invoke(HttpContext httpContext)
        {
            try
            {
                if (_options.ForceHttpPost && !HttpMethods.IsPost(httpContext.Request.Method))
                {
                    httpContext.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
                    return;
                }

                JsonRpcRequest request;
                try
                {
                    request = await CreateRequestAsync(httpContext, _server.Serializer);
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(JsonRpcLogEvents.ParseError, ex, "Failed to parse request");

                    if (!httpContext.Response.HasStarted && !httpContext.RequestAborted.IsCancellationRequested)
                    {
                        await SendResponse(httpContext, new JsonRpcResponse(null, JsonRpcError.PARSE_ERROR, errorData : ex.ToDiagnosticString()));
                    }
                    return;
                }

                var response = await _server.ProcessAsync(httpContext.RequestServices, request);

                if (request.IsNotification)
                {
                    httpContext.Response.StatusCode = 204;
                }
                else
                {
                    await SendResponse(httpContext, response);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(JsonRpcLogEvents.InternalError, ex, "Failed to send response");

                if (!httpContext.Response.HasStarted && !httpContext.RequestAborted.IsCancellationRequested)
                {
                    await SendResponse(httpContext, new JsonRpcResponse(null, JsonRpcError.INTERNAL_ERROR, errorData : ex.Unwrap().ToDiagnosticString()));
                }
            }
        }