Esempio n. 1
0
        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="args"></param>
        public void Call(CallEventArgs args)
        {
            //计数按方法
            var thisKey = string.Format("{0}${1}${2}${3}", args.Caller.AppName, args.Caller.ServiceName, args.Caller.MethodName, args.Caller.Parameters);
            var callKey = IoCHelper.GetMD5String(thisKey);

            lock (hashtable.SyncRoot)
            {
                if (!hashtable.ContainsKey(callKey))
                {
                    var counterInfo = new CounterInfo
                    {
                        AppName     = args.Caller.AppName,
                        ServiceName = args.Caller.ServiceName,
                        MethodName  = args.Caller.MethodName,
                        Parameters  = args.Caller.Parameters,
                        NeedReset   = false,
                        Count       = 1
                    };

                    hashtable[callKey] = counterInfo;

                    return;
                }
            }

            var counter = hashtable[callKey] as CounterInfo;

            if (counter.NeedReset)
            {
                //重置计数器
                hashtable.Remove(callKey);

                //如果调用次数超过最大允许数,则提示警告
                if (counter.Count >= maxCount)
                {
                    var warning = new WarningException(string.Format("【{0}】 One minute call service ({1}, {2}) {3} times more than {4} times.\r\nParameters => {5}",
                                                                     counter.AppName, counter.ServiceName, counter.MethodName, counter.Count, maxCount, counter.Parameters));

                    //内部异常
                    var error = new IoCException(string.Format("【{0}】 One minute call service ({1}) {2} times.",
                                                               counter.AppName, counter.ServiceName, counter.Count), warning);

                    //抛出异常
                    args.Error = error;
                }
            }
            else
            {
                //计数器加1
                counter.Count++;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="args"></param>
        public void Call(CallEventArgs args)
        {
            //计数按方法
            var thisKey = string.Format("{0}${1}${2}${3}", args.Caller.AppName, args.Caller.ServiceName, args.Caller.MethodName, args.Caller.Parameters);
            var callKey = IoCHelper.GetMD5String(thisKey);

            lock (hashtable.SyncRoot)
            {
                if (!hashtable.ContainsKey(callKey))
                {
                    var counterInfo = new CounterInfo
                    {
                        AppName = args.Caller.AppName,
                        ServiceName = args.Caller.ServiceName,
                        MethodName = args.Caller.MethodName,
                        Parameters = args.Caller.Parameters,
                        NeedReset = false,
                        Count = 1
                    };

                    hashtable[callKey] = counterInfo;

                    return;
                }
            }

            var counter = hashtable[callKey] as CounterInfo;
            if (counter.NeedReset)
            {
                //重置计数器
                hashtable.Remove(callKey);

                //如果调用次数超过最大允许数,则提示警告
                if (counter.Count >= maxCount)
                {
                    var warning = new WarningException(string.Format("【{0}】 One minute call service ({1}, {2}) {3} times more than {4} times.\r\nParameters => {5}",
                      counter.AppName, counter.ServiceName, counter.MethodName, counter.Count, maxCount, counter.Parameters));

                    //内部异常
                    var error = new IoCException(string.Format("【{0}】 One minute call service ({1}) {2} times.",
                        counter.AppName, counter.ServiceName, counter.Count), warning);

                    //抛出异常
                    args.Error = error;
                }
            }
            else
            {
                //计数器加1
                counter.Count++;
            }
        }