Beispiel #1
0
        public static string GetRpcMethodContext(string pInterfaceName, string pFunctionName, object[] pParames)
        {
            RpcMethod rpcMethod = new RpcMethod(pInterfaceName, pFunctionName, pParames);

            return(rpcMethod._mElement.ToString());
        }
Beispiel #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);
                    }
                }
            }
        }