Example #1
0
 /// <summary>
 /// 远程过程调用
 /// </summary>
 /// <param name="pInterfaceName">接口名称</param>
 /// <param name="pFunctionName">方法名称</param>
 /// <param name="pParames">参数集合</param>
 /// <param name="pCallback">回调函数,方法调用结束</param>
 /// <param name="pReturnOnlyOne">true - 确保相同的方法多次调用后,只返回最后一次调用的结果。默认为 false</param>
 public static void Invoke(string pInterfaceName, string pFunctionName, object[] pParames, RpcResultCallback pCallback, bool pReturnOnlyOne = false)
 {
     //@?
     //var paramList = new List<object>
     //                    {
     //                        WMonitorConfig.Current.CatalogServiceIp,
     //                        WMonitorConfig.Current.CatalogServicePort,
     //                        WMonitorConfig.Current.RemotingToken
     //                    };
     //if (pParames != null)
     //{
     //    paramList.Add(JsonConvert.SerializeObject(pParames));
     //}
     //RpcManager.Current.Invoke(pInterfaceName, pFunctionName, paramList.ToArray(), delegate(RpcResult pResult)
     //{
     //    try
     //    {
     //        if (pResult.Code == RpcResultCode.TokenInvalid
     //            || pResult.Code == RpcResultCode.SocketError)
     //        {
     //            WaitIndicatorEx.WaitMessage(pResult.Message);
     //        }
     //        else
     //        {
     //            switch (pResult.Code)
     //            {
     //                case RpcResultCode.Fail:
     //                    {
     //                        Utile.ShowUnhandledException(pResult.Message);
     //                        try
     //                        {
     //                            if (pCallback != null)
     //                            {
     //                                pCallback(pResult);
     //                            }
     //                        }
     //                        catch
     //                        {
     //                        }
     //                    }
     //                    break;
     //                default:
     //                    {
     //                        if (pCallback != null)
     //                        {
     //                            pCallback(pResult);
     //                        }
     //                    }
     //                    break;
     //            }
     //            if (Current.RpcInvokeCompleted != null)
     //                Current.RpcInvokeCompleted(pResult);
     //        }
     //    }
     //    catch (Exception ex)
     //    {
     //        throw new Exception(string.Format("接口:{0},方法:{1},回调函数内发生异常", pInterfaceName, pFunctionName), ex);
     //    }
     //}, pReturnOnlyOne);
 }
Example #2
0
        public void Invoke(string pInterfaceName, string pFunctionName, object[] pParames, RpcResultCallback pCallback, bool pReturnOnlyOne = false)
        {
            BatchInvokeCallbackObject bInvokeCallbackObject = null;

            try
            {
                //是否为批量调用
                if (_currentBatchInvokeCallbackObject != null)
                {
                    if (BatchInvokeQueue.ContainsKey(_currentBatchInvokeCallbackObject))
                    {
                        bInvokeCallbackObject = BatchInvokeQueue[_currentBatchInvokeCallbackObject];
                        if (bInvokeCallbackObject != null)
                        {
                            bInvokeCallbackObject.AddInvokeCount();
                        }
                    }
                }
                //判断在多次调用同一个方法时,是否仅返回一次结果
                string code = pInterfaceName + pFunctionName;
                lock (LockOnlyOne)
                {
                    if (pReturnOnlyOne)
                    {
                        if (InvokeCountDic.ContainsKey(code))
                        {
                            //累加调用方法次数
                            InvokeCountDic[code]++;
                        }
                        else
                        {
                            //初次调用该方法赋值1,该方法执行返回后再减1
                            InvokeCountDic[code] = 1;
                        }
                    }
                }
                SynchronizationContext syncContext = SynchronizationContext.Current;
                string url       = "";//@? Utile.GetHostUrl();
                var    remoting  = new Uri(string.Format("{0}/RemotingHandler.ashx", url.ToString(CultureInfo.InvariantCulture)));
                var    hwRequest = (HttpWebRequest)WebRequest.Create(remoting);
                hwRequest.Method      = "POST";
                hwRequest.ContentType = "application/x-www-form-urlencoded";
                string methodContext = RpcMethod.GetRpcMethodContext(pInterfaceName, pFunctionName, pParames);
                var    rpcState      = new RpcState
                {
                    WebRequest                = hwRequest,
                    ResultCallback            = pCallback,
                    SyncContext               = syncContext,
                    MethodContext             = methodContext,
                    BatchInvokeCallbackObject = bInvokeCallbackObject,
                    IsOnlyOne   = pReturnOnlyOne,
                    OnlyOneCode = code
                };
                hwRequest.BeginGetRequestStream(OnGetRequestStream, rpcState);
            }
            catch (Exception ex)
            {
                if (pCallback != null)
                {
                    var result = new RpcResult();
                    result.SetException(ex);
                    pCallback(result);
                    if (bInvokeCallbackObject != null)
                    {
                        bInvokeCallbackObject.RemoveInvokeCount(result);
                    }
                }
            }
        }