Esempio n. 1
0
 public override void Intercept(Castle.DynamicProxy.IInvocation invocation)
 {
     invocation.Proceed();
     if (invocation.ReturnValue == null)
     {
         Entity entity = invocation.InvocationTarget.CastToType <Entity>();
         if (entity.IsNotNull() || !invocation.Method.IsGetter())
         {
             var mInfo = entity.TypeDesription.Methods[invocation.Method.ToString()];
             if (mInfo != null && mInfo.RespositoryType.IsPresent())
             {
                 var repository = (IRelatedRepository)scope.Resolve(mInfo.RespositoryType.Get());
                 invocation.ReturnValue = repository.Get(entity);
                 if (mInfo.Property.SetMethod != null)
                 {
                     if (!mInfo.Property.SetMethod.Access.Setter.IsPresent())
                     {
                         throw new InvalidOperationException();
                     }
                     mInfo.Property.SetMethod.Access.Setter.Get()(entity, invocation.ReturnValue);
                 }
             }
             else
             {
                 throw new InvalidOperationException();
             }
         }
         else
         {
             throw new InvalidOperationException();
         }
     }
 }
Esempio n. 2
0
 public void Intercept(Castle.DynamicProxy.IInvocation invocation)
 {
     if (invocation.Arguments[0].GetType() == typeof(int))
     {
         invocation.Arguments[0] = (int)invocation.Arguments[0] + 1;
     }
     invocation.Proceed();
 }
        public override void Intercept(Castle.DynamicProxy.IInvocation invocation)
        {
            var methodName = string.Format($"{invocation.Method.ReflectedType.FullName}.{invocation.Method.Name}");
            var arguments  = invocation.Arguments.ToList();
            var key        = $"{methodName}({string.Join(",", arguments.Select(x => x?.ToString() ?? "<Null>"))})";

            if (_cacheManager.IsAdd(key))
            {
                invocation.ReturnValue = _cacheManager.Get(key);
                return;
            }
            invocation.Proceed();
            _cacheManager.Add(key, invocation.ReturnValue, _duration);
        }
        public object Proceed()
        {
            object retVal = null;

            if (_currentIndex + 1 <= _interceptors.Length)
            {
                retVal = _interceptors[_currentIndex++].Invoke(this);
            }
            else
            {
                retVal = _innerInvocation.Proceed(_arguments);
            }

            return(retVal);
        }
Esempio n. 5
0
 public void Intercept(IInvocation invocation) {
     if (currentElement.Value.Value != null)
         currentElement.Value.Value.Stop();
     var sw = new Stopwatch();
     var newChild = currentElement.AddChild(KV.Create(invocation.MethodInvocationTarget, sw));
     currentElement = newChild;
     sw.Start();
     try {
         invocation.Proceed();
     } finally {
         sw.Stop();
         currentElement = currentElement.Parent;
         if (currentElement.Value.Value != null)
             currentElement.Value.Value.Start();
     }
 }
Esempio n. 6
0
            public void Intercept(IInvocation invocation)
            {
                if (currentElement.Value.Value != null)
                {
                    currentElement.Value.Value.Stop();
                }
                var sw       = new Stopwatch();
                var newChild = currentElement.AddChild(KV(invocation.MethodInvocationTarget, sw));

                currentElement = newChild;
                sw.Start();
                try {
                    invocation.Proceed();
                } finally {
                    sw.Stop();
                    currentElement = currentElement.Parent;
                    if (currentElement.Value.Value != null)
                    {
                        currentElement.Value.Value.Start();
                    }
                }
            }
Esempio n. 7
0
        public void Intercept(Castle.DynamicProxy.IInvocation invocation)
        {
            bool initTransaction = NHSessionManager.Instance.BeginTransaction();

            try
            {
                invocation.Proceed();

                if (invocation.ReturnValue is INHibernateProxy)
                {
                    NHSessionManager.Instance.GetSession().GetSessionImplementation().PersistenceContext.Unproxy(invocation.ReturnValue);
                }

                if (initTransaction)
                {
                    NHSessionManager.Instance.CommitTransaction();
                }
            }
            catch (ApplicationException)
            {
                if (initTransaction)
                {
                    NHSessionManager.Instance.RollbackTransaction();
                }

                throw;
            }
            catch (Exception e)
            {
                if (initTransaction)
                {
                    NHSessionManager.Instance.RollbackTransaction();
                }

                throw e;
            }
        }
Esempio n. 8
0
        public void Intercept(Castle.DynamicProxy.IInvocation invocation)
        {
            EntityRow entity = invocation.InvocationTarget.CastToType <EntityRow>();

            if (entity.IsNotNull() && entity.Row.IsNotNull())
            {
                string propertyName = invocation.Method.Name;
                if (propertyName.StartsWith("get_") || propertyName.StartsWith("set_"))
                {
                    propertyName = propertyName.Substring(4, propertyName.Length - 4);
                }
                if (invocation.Method.Name.StartsWith("get_"))
                {
                    KeyValuePair <string, EntityProperty> column = entity.GetDataTableColumn(propertyName);
                    if (!string.IsNullOrEmpty(column.Key))
                    {
                        if (entity.Row.Table.Columns.Contains(column.Key))
                        {
                            if (entity.Row[column.Key] != System.DBNull.Value)
                            {
                                invocation.ReturnValue = entity.Row[column.Key];
                            }
                            else
                            {
                                invocation.ReturnValue = column.Value.PropertyType.GetDefaultValue();
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    else
                    {
                        invocation.Proceed();
                    }
                }
                else if (invocation.Method.Name.StartsWith("set_") && invocation.Arguments.Count() > 0)
                {
                    KeyValuePair <string, EntityProperty> column = entity.GetDataTableColumn(propertyName);
                    if (!string.IsNullOrEmpty(column.Key))
                    {
                        if (entity.Row.Table.Columns.Contains(column.Key))
                        {
                            if (invocation.Arguments[0] != null)
                            {
                                entity.Row[column.Key] = invocation.Arguments[0];
                            }
                            else
                            {
                                entity.Row[column.Key] = System.DBNull.Value;
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    else
                    {
                        invocation.Proceed();
                    }
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Intercept
        /// </summary>
        /// <param name="invocation"></param>
        public virtual void Intercept(Castle.DynamicProxy.IInvocation invocation)
        {
            var startTime = new System.Diagnostics.Stopwatch();

            startTime.Start();
            var meta       = this.MetaData[invocation.Method.Name];
            var methodName = meta.FullName;
            var argsObj    = invocation.Arguments;
            //var argsObjLog = new System.Collections.Generic.Dictionary<string, System.Tuple<dynamic, MetaLogger>>(argsObj.Length);
            //var argsObjLog = new System.Collections.Generic.List<argsLog>(argsObj.Length);
            //var argsObjLog = argsObj.Select(c => new argsLog { }).ToList();
            //var argsObjLogHasIArg = new System.Collections.Generic.Dictionary<string, bool>(argsObj.Length);
            var logType = LoggerType.Record;
            //==================================//
            var iArgGroup = meta.GroupDefault;
            var iArgs     = Bind.GetIArgs(meta.IArgs, argsObj, iArgGroup);

            if (0 < iArgs.Count)
            {
                var group = iArgs[meta.IArgs[0].Position].Group;
                if (!System.String.IsNullOrEmpty(group))
                {
                    iArgGroup = group;
                }
            }

            var args = meta.ArgAttrs[iArgGroup];

            //var argsObjLog = args.Args.Select(c => new argsLog {Key = c.Name, Value = c.HasIArg ? iArgs[c.Position] : argsObj[c.Position], Logger = c.MetaLogger }).ToList();

            try
            {
                foreach (var item in args.Args)
                {
                    IResult result = null;
                    var     value  = argsObj[item.Position];
                    var     iArgIn = item.HasIArg ? iArgs[item.Position].In : null;
                    //argsObjLog.Add(item.Name, System.Tuple.Create(item.HasIArg ? iArgs[item.Position] : value, item.MetaLogger));
                    //argsObjLog.Add(new argsLog { Key = item.Name, Value = item.HasIArg ? iArgs[item.Position] : value, Logger = item.MetaLogger });
                    //argsObjLogHasIArg.Add(item.Name, item.HasIArg);

                    for (int i = 0; i < item.ArgAttr.Count; i++)
                    {
                        var argAttr = item.ArgAttr[i];

                        result = argAttr.Proces(item.HasIArg ? iArgIn : value);

                        if (1 > result.State)
                        {
                            invocation.ReturnValue = Bind.GetReturnValue(result, meta); logType = LoggerType.Error; return;
                        }

                        //========================================//

                        if (result.HasData)
                        {
                            if (!item.HasIArg)
                            {
                                argsObj[item.Position] = result.Data;
                            }
                            else
                            {
                                if (i < item.ArgAttr.Count - 1)
                                {
                                    iArgIn = result.Data;
                                }
                                else
                                {
                                    iArgs[item.Position].Out = result.Data;
                                }
                            }
                        }
                    }

                    //========================================//
                    object currentValue = item.HasIArg ?
                                          ((null != result && result.HasData) ? result.Data : iArgs[item.Position].Out) :
                                          ((null != result && result.HasData) ? result.Data : value);
                    //========================================//
                    //item.HasIArg &&
                    if (null == currentValue)
                    {
                        continue;
                    }

                    var isUpdate = false;

                    result = ArgsResult(item.ArgAttrChild, args.CommandAttr.OnlyName, ref currentValue, ref isUpdate);
                    if (null != result)
                    {
                        invocation.ReturnValue = Bind.GetReturnValue(result, meta); logType = LoggerType.Error; return;
                    }

                    if (item.HasIArg && isUpdate)
                    {
                        iArgs[item.Position].Out = currentValue;
                    }
                }

                //===============================//
                startTime.Restart();
                invocation.Proceed();
            }
            catch (System.Exception ex)
            {
                invocation.ReturnValue = Bind.GetReturnValue(0, System.Convert.ToString(Utils.Help.ExceptionWrite(ex)), meta, ResultType); logType = LoggerType.Exception;
            }
            finally
            {
                startTime.Stop();
                var total = Utils.Help.Scale(startTime.Elapsed.TotalSeconds, 3);

                if (null != this.Logger)
                {
                    if (meta.HasIResult && 0 > ((IResult)invocation.ReturnValue).State)
                    {
                        logType = LoggerType.Error;
                    }

                    var argsObjLog = args.Args.Select(c => new ArgsLog {
                        name = c.Name, value = c.HasIArg ? iArgs[c.Position] : argsObj[c.Position], logger = c.MetaLogger, hasIArg = c.HasIArg
                    }).ToList();

                    var logObjs = LoggerSet(logType, meta.MetaLogger, argsObjLog, argsObjLog.ToDictionary(c => c.name, c => c.hasIArg), out bool canWrite, out bool canResult);

                    if (canWrite)
                    {
                        System.Threading.Tasks.Task.Run(() => this.Logger(new LoggerData {
                            Type = logType, Value = logObjs, Result = canResult ? invocation.ReturnValue : null, Time = total, Member = methodName, Group = args.CommandAttr.Group
                        }));
                    }
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Intercept
        /// </summary>
        /// <param name="invocation"></param>
        public virtual async void Intercept(Castle.DynamicProxy.IInvocation invocation)
        {
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            var meta       = this.MetaData[invocation.Method.Name];
            var methodName = meta.FullName;
            var argsObj    = invocation.Arguments;
            //var argsObjLog = new System.Collections.Generic.Dictionary<string, System.Tuple<dynamic, MetaLogger>>(argsObj.Length);
            //var argsObjLog = new System.Collections.Generic.List<argsLog>(argsObj.Length);
            //var argsObjLog = argsObj.Select(c => new argsLog { }).ToList();
            //var argsObjLogHasIArg = new System.Collections.Generic.Dictionary<string, bool>(argsObj.Length);
            var logType = LoggerType.Record;
            //==================================//
            var iArgGroup = meta.GroupDefault;
            var iArgs     = Bind.GetIArgs(meta.IArgs, argsObj, iArgGroup);

            if (0 < iArgs.Count)
            {
                var group = iArgs[meta.IArgs[0].Position].Group;
                if (!string.IsNullOrEmpty(group))
                {
                    iArgGroup = group;
                }
            }

            //var args = meta.ArgAttrs[iArgGroup];
            //if (!meta.ArgAttrs.TryGetValue(iArgGroup, out ArgAttrs args))
            //{
            //    invocation.ReturnValue = Bind.GetReturnValue(Bind.CmdError(ResultType, invocation.Method.Name), meta);
            //    return;
            //}

            //var group = meta.ArgAttrs[iArgGroup];
            if (!meta.CommandGroup.TryGetValue(iArgGroup, out CommandAttribute command))
            {
                invocation.ReturnValue = Bind.GetReturnValue(Bind.CmdError(ResultType, invocation.Method.Name), meta);
                return;
            }

            //var argsObjLog = args.Args.Select(c => new argsLog {Key = c.Name, Value = c.HasIArg ? iArgs[c.Position] : argsObj[c.Position], Logger = c.MetaLogger }).ToList();
            dynamic returnValue = null;

            try
            {
                foreach (var item in meta.Args)
                {
                    //if (meta.UseTypePosition.ContainsKey(item.Position) && !item.HasIArg) { continue; }

                    IResult result = null;
                    var     value  = argsObj[item.Position];
                    var     iArgIn = item.HasIArg ? iArgs[item.Position].In : null;
                    //argsObjLog.Add(item.Name, System.Tuple.Create(item.HasIArg ? iArgs[item.Position] : value, item.MetaLogger));
                    //argsObjLog.Add(new argsLog { Key = item.Name, Value = item.HasIArg ? iArgs[item.Position] : value, Logger = item.MetaLogger });
                    //argsObjLogHasIArg.Add(item.Name, item.HasIArg);
                    //var argAttrs = item.ArgAttr.OrderByDescending(c => c.Value.State);
                    var attrs = item.Group[iArgGroup].Attrs;

                    var first = attrs.First;

                    while (NodeState.DAT == first.State)
                    {
                        var argAttr = first.Value;

                        result = argAttr.Meta.HasProcesIArg ? await argAttr.Proces(item.HasIArg?iArgIn : value, item.HasIArg?iArgs[item.Position] : null) : await argAttr.Proces(item.HasIArg?iArgIn : value);

                        if (1 > result.State)
                        {
                            logType                = LoggerType.Error;
                            returnValue            = result;
                            invocation.ReturnValue = Bind.GetReturnValue(result, meta);
                            return;
                        }

                        //========================================//

                        first = first.Next;

                        if (result.HasData)
                        {
                            if (!item.HasIArg)
                            {
                                argsObj[item.Position] = result.Data;
                            }
                            else
                            {
                                //if (i < attrs.Count - 1)
                                if (NodeState.DAT == first.State)
                                {
                                    iArgIn = result.Data;
                                }
                                else
                                {
                                    iArgs[item.Position].Out = result.Data;
                                }
                            }
                        }
                    }

                    //foreach (var argAttr in attrs)
                    ////for (int i = 0; i < item.ArgAttr.Count; i++)
                    //{
                    //    result = argAttr.Meta.HasProcesIArg ? await argAttr.Proces(item.HasIArg ? iArgIn : value, item.HasIArg ? iArgs[item.Position] : null) : await argAttr.Proces(item.HasIArg ? iArgIn : value);

                    //    if (1 > result.State)
                    //    {
                    //        logType = LoggerType.Error;
                    //        returnValue = result;
                    //        invocation.ReturnValue = Bind.GetReturnValue(result, meta);
                    //        return;
                    //    }

                    //    //========================================//

                    //    if (result.HasData)
                    //    {
                    //        if (!item.HasIArg)
                    //        {
                    //            argsObj[item.Position] = result.Data;
                    //        }
                    //        else
                    //        {
                    //            if (i < attrs.Count - 1)
                    //            {
                    //                iArgIn = result.Data;
                    //            }
                    //            else
                    //            {
                    //                iArgs[item.Position].Out = result.Data;
                    //            }
                    //        }
                    //    }

                    //    i++;
                    //}

                    //========================================//

                    object currentValue = item.HasIArg ?
                                          ((null != result && result.HasData) ? result.Data : iArgs[item.Position].Out) :
                                          ((null != result && result.HasData) ? result.Data : value);
                    //========================================//
                    //item.HasIArg &&
                    if (null == currentValue)
                    {
                        continue;
                    }

                    //var isUpdate = false;

                    result = await ArgsResult2(iArgGroup, item.ArgAttrChild, command.OnlyName, currentValue);

                    //if (null != result)
                    if (1 > result.State)
                    {
                        logType                = LoggerType.Error;
                        returnValue            = result;
                        invocation.ReturnValue = Bind.GetReturnValue(result, meta);
                        return;
                    }

                    if (item.HasIArg && result.Data)
                    {
                        iArgs[item.Position].Out = currentValue;
                    }
                }

                //===============================//
                //watch.Restart();
                invocation.Proceed();
                returnValue = invocation.ReturnValue;
            }
            catch (System.Exception ex)
            {
                ex      = ex.ExceptionWrite();
                logType = LoggerType.Exception;
                if (!meta.HasAsync)
                {
                    returnValue = ResultFactory.ResultCreate(ResultType, 0, System.Convert.ToString(ex));
                }
                invocation.ReturnValue = !meta.HasAsync ? Bind.GetReturnValue(0, System.Convert.ToString(ex), meta, ResultType) : System.Threading.Tasks.Task.FromException(ex);
            }
            finally
            {
                if (meta.HasAsync)
                {
                    var task = invocation.ReturnValue as System.Threading.Tasks.Task;

                    if (meta.HasIResult)
                    {
                        invocation.ReturnValue = task.ContinueWith <IResult>(c => Async <IResult>(iArgGroup, c, ResultType, meta, returnValue, logType, iArgs, argsObj, methodName, this.Logger, watch));
                    }
                    else
                    {
                        invocation.ReturnValue = task.ContinueWith(c => Async <dynamic>(iArgGroup, c, ResultType, meta, returnValue, logType, iArgs, argsObj, methodName, this.Logger, watch));
                    }
                }
                else
                {
                    Finally(iArgGroup, meta, returnValue, logType, iArgs, argsObj, methodName, this.Logger, watch);
                }
            }
        }
Esempio n. 11
0
 /// <summary>
 /// 调用
 /// </summary>
 public void Proceed()
 {
     invocation.Proceed();
 }
Esempio n. 12
0
 public void Intercept(Castle.DynamicProxy.IInvocation invocation)
 {
     invocation.Proceed();
 }
Esempio n. 13
0
 public void Intercept(Castle.DynamicProxy.IInvocation invocation)
 {
     Console.WriteLine("-- Call to " + invocation.Method);
     invocation.Proceed();
 }