/// <summary> /// 获取Rpc调用器 /// </summary> /// <param name="method">JSON-RPC方法名</param> /// <param name="option">自定义选项</param> private static RpcInvoker GetRpcInvoker(string method, RpcOption option) { string serviceAddress = (option != null && !string.IsNullOrWhiteSpace(option.ServiceAddress)) ? option.ServiceAddress : Configmanager.GetServiceUrlFromConfig (method); RpcInvoker invoker = null; if (string.Equals(serviceAddress, "local", StringComparison.InvariantCultureIgnoreCase)) { invoker = new LocalRpcInvoker(); } else if (serviceAddress.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) || serviceAddress.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)) { invoker = new HttpRpcInvoker(); } else { throw new NotSupportedException("不支持的Rpc服务地址类型:" + serviceAddress); } invoker.ServiceAddress = serviceAddress; invoker.Option = option; return(invoker); }
/// <summary> /// 调用JSON-RPC服务(同步),泛型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="method">JSON-RPC方法名</param> /// <param name="option">自定义选项</param> /// <param name="args">JSON-RPC方法接收的参数,此参数为可变数组</param> /// <returns></returns> public static T CallWithOption <T>(string method, RpcOption option, params object[] args) { var rpcInvoker = GetRpcInvoker(method, option); var responseContext = rpcInvoker.Invoke <T>(method, args); if (responseContext.Error != null) { throw responseContext.Error; } return(responseContext.Result); }
/// <summary> /// 批量调用JSON-RPC服务 /// </summary> /// <param name="jsonRpcRequests">批量JSON-RPC请求列表</param> /// <param name="option">参数列表</param> /// <returns></returns> public static List <JsonRpcResponseContext> BatchCall(List <JsonRpcRequestContext> jsonRpcRequests, RpcOption option = null) { if (jsonRpcRequests == null || jsonRpcRequests.Count == 0) { throw new ArgumentNullException("jsonRpcRequests", "jsonRpcRequests is null or empty"); } var rpcInvoker = GetRpcInvoker(jsonRpcRequests[0].Method, option); return(rpcInvoker.BatchInvoke(jsonRpcRequests)); }
/// <summary> /// 调用JSON-RPC服务(同步) /// </summary> /// <param name="method">JSON-RPC方法名</param> /// <param name="option">自定义选项</param> /// <param name="args">JSON-RPC方法接收的参数,此参数为可变数组</param> public static void CallWithOption(string method, RpcOption option, params object[] args) { CallWithOption <object>(method, option, args); }