示例#1
0
        private async Task <JsonRpcResponse> ExecuteAsync(JsonRpcRequest request, string methodName, MethodInfo method, object module)
        {
            Console.WriteLine($"{methodName}");

            var expectedParameters = method.GetParameters();
            var providedParameters = request.Params;
            int missingParamsCount = expectedParameters.Length - (providedParameters?.Length ?? 0) + providedParameters?.Count(string.IsNullOrWhiteSpace) ?? 0;

            if (missingParamsCount != 0)
            {
                bool incorrectParametersCount = missingParamsCount != 0;
                if (missingParamsCount > 0)
                {
                    incorrectParametersCount = false;
                    for (int i = 0; i < missingParamsCount; i++)
                    {
                        if (!expectedParameters[expectedParameters.Length - missingParamsCount + i].IsOptional)
                        {
                            incorrectParametersCount = true;
                            break;
                        }
                    }
                }

                if (incorrectParametersCount)
                {
                    return(GetErrorResponse(ErrorType.InvalidParams, $"Incorrect parameters count, expected: {expectedParameters.Length}, actual: {expectedParameters.Length - missingParamsCount}", request.Id, methodName));
                }
            }

            //prepare parameters
            object[] parameters = null;
            if (expectedParameters.Length > 0)
            {
                parameters = DeserializeParameters(expectedParameters, providedParameters, missingParamsCount);
                if (parameters == null)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Incorrect JSON RPC parameters when calling {methodName}: {string.Join(", ", providedParameters)}");
                    }
                    return(GetErrorResponse(ErrorType.InvalidParams, "Incorrect parameters", request.Id, methodName));
                }
            }

            //execute method
            IResultWrapper resultWrapper    = null;
            var            invocationResult = method.Invoke(module, parameters);

            if (invocationResult is IResultWrapper wrapper)
            {
                resultWrapper = wrapper;
            }
            else if (invocationResult is Task task)
            {
                await task;
                resultWrapper = task.GetType().GetProperty("Result").GetValue(task) as IResultWrapper;
            }

            if (resultWrapper is null)
            {
                string errorMessage = $"Method {methodName} execution result does not implement IResultWrapper";
                if (_logger.IsError)
                {
                    _logger.Error(errorMessage);
                }
                return(GetErrorResponse(ErrorType.InternalError, errorMessage, request.Id, methodName));
            }

            Result result = resultWrapper.GetResult();

            if (result == null || result.ResultType == ResultType.Failure)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Error during method: {methodName} execution: {result?.Error ?? "no result"}");
                }
                return(GetErrorResponse(resultWrapper.GetErrorType(), resultWrapper.GetResult().Error, request.Id, methodName, resultWrapper.GetData()));
            }

            return(GetSuccessResponse(resultWrapper.GetData(), request.Id));
        }
示例#2
0
 private async Task <JsonRpcResponse> ExecuteAsync(JsonRpcRequest request, string methodName, (MethodInfo Info, bool ReadOnly) method)