Esempio n. 1
0
        /// <summary>
        ///		同步调用Rpc接口, 仅用于希望同步操作的场合
        /// </summary>
        /// <param name="methodName">方法名</param>
        /// <typeparam name="TArgs">调用的参数类型</typeparam>
        /// <param name="args">调用的参数</param>
        /// <typeparam name="TResults">返回参数类型</typeparam>
        /// <param name="timeoutMs">超时时间, 毫秒ms</param>
        /// <returns>Rpc调用返回值</returns>
        /// <remarks>一般来说同步Rpc的允许的场合包括:
        ///		1. 读取配置
        ///		2. 在固定线程中的, 从队列中取出数据的并通过Rpc处理, 避免对服务的压力并保持顺序的一致性
        ///		3. 单元测试
        /// </remarks>
        public TResults Invoke <TArgs, TResults>(string methodName, TArgs args, int timeoutMs)
        {
            var a = new SyncInvoker <RpcClientContext>();

            BeginInvoke <TArgs>(methodName, args, a.Callback);

            if (a.Wait(timeoutMs))
            {
                return(a.Context.EndInvoke <TResults>());
            }
            else
            {
                throw new RpcException(RpcErrorCode.TransactionTimeout, "", "TransactionTimeout", null);
            }
        }
Esempio n. 2
0
        public static JsonRpcResponse Invoke(string serviceUri, string module, string method, string reqBody)
        {
            JsonRpcResponse resp = null;
            JsonRpcHttpClientTransaction proxy = new JsonRpcHttpClientTransaction();


            SyncInvoker    sync   = new SyncInvoker();
            JsonRpcRequest rpcReq = new JsonRpcRequest(serviceUri, reqBody);

            rpcReq.AddHeader("UU-REQUEST-MODULE", module);
            rpcReq.AddHeader("UU-REQUEST-ACTION", method);
            rpcReq.AddHeader("UU-AUTH-TYPE", "2");
            proxy.SendRequest(rpcReq, (rpcResponse) =>
            {
                resp = rpcResponse;
                sync.Callback();
            });

            sync.Wait(1000 * 80);
            return(resp);
        }