public RpcResponseBase InvokeRequest(RpcRequest request, RpcRoute route) { try { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (route == null) { throw new ArgumentNullException(nameof(route)); } } catch (ArgumentNullException ex) // Dont want to throw any exceptions when doing async requests { return(this.GetUnknownExceptionReponse(request, ex)); } this.Logger?.LogVerbose($"Invoking request with id '{request.Id}'"); RpcResponseBase rpcResponse; try { if (!string.Equals(request.JsonRpcVersion, "2.0")) { throw new RpcInvalidRequestException("Request must be jsonrpc version '2.0'"); } object[] parameterList; RpcMethod rpcMethod = this.GetMatchingMethod(route, request, out parameterList); this.Logger?.LogVerbose($"Attempting to invoke method '{request.Method}'"); object result = rpcMethod.Invoke(parameterList); this.Logger?.LogVerbose($"Finished invoking method '{request.Method}'"); rpcResponse = new RpcResultResponse(request.Id, result); } catch (RpcException ex) { this.Logger?.LogError("An Rpc error occurred. Returning an Rpc error response", ex); RpcError error = new RpcError(ex); rpcResponse = new RpcErrorResponse(request.Id, error); } catch (Exception ex) { rpcResponse = this.GetUnknownExceptionReponse(request, ex); } if (request.Id != null) { this.Logger?.LogVerbose($"Finished request with id '{request.Id}'"); //Only give a response if there is an id return(rpcResponse); } this.Logger?.LogVerbose($"Finished request with no id. Not returning a response"); return(null); }
/// <summary> /// Call the incoming Rpc request method and gives the appropriate response /// </summary> /// <param name="request">Rpc request</param> /// <param name="route">Rpc route that applies to the current request</param> /// <param name="serviceProvider">(Optional)IoC Container for rpc method controllers</param> /// <param name="jsonSerializerSettings">Json serialization settings that will be used in serialization and deserialization for rpc requests</param> /// <returns>An Rpc response for the request</returns> public RpcResponse InvokeRequest(RpcRequest request, RpcRoute route, IServiceProvider serviceProvider = null, JsonSerializerSettings jsonSerializerSettings = null) { try { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (route == null) { throw new ArgumentNullException(nameof(route)); } } catch (ArgumentNullException ex) // Dont want to throw any exceptions when doing async requests { return(this.GetUnknownExceptionReponse(request, ex)); } this.Logger?.LogDebug($"Invoking request with id '{request.Id}'"); RpcResponse rpcResponse; try { if (!string.Equals(request.JsonRpcVersion, JsonRpcContants.JsonRpcVersion)) { throw new RpcInvalidRequestException($"Request must be jsonrpc version '{JsonRpcContants.JsonRpcVersion}'"); } object[] parameterList; RpcMethod rpcMethod = this.GetMatchingMethod(route, request, out parameterList, serviceProvider, jsonSerializerSettings); this.Logger?.LogDebug($"Attempting to invoke method '{request.Method}'"); object result = rpcMethod.Invoke(parameterList); this.Logger?.LogDebug($"Finished invoking method '{request.Method}'"); JsonSerializer jsonSerializer = JsonSerializer.Create(jsonSerializerSettings); JToken resultJToken = result != null?JToken.FromObject(result, jsonSerializer) : null; rpcResponse = new RpcResponse(request.Id, resultJToken); } catch (RpcException ex) { this.Logger?.LogException(ex, "An Rpc error occurred. Returning an Rpc error response"); RpcError error = new RpcError(ex, this.ShowServerExceptions); rpcResponse = new RpcResponse(request.Id, error); } catch (Exception ex) { rpcResponse = this.GetUnknownExceptionReponse(request, ex); } if (request.Id != null) { this.Logger?.LogDebug($"Finished request with id '{request.Id}'"); //Only give a response if there is an id return(rpcResponse); } this.Logger?.LogDebug($"Finished request with no id. Not returning a response"); return(null); }