Exemplo n.º 1
0
 // Record time spent executing the method
 public override void OnInvocation(MethodInvocationEventArgs eventArgs)
 {
   var start = DateTime.Now;
   eventArgs.Proceed();
   var timeSpent = (DateTime.Now - start).TotalMilliseconds;
   
   if (LogDebug)
   {
   Log.DebugFormat(
   "Method [{0}{1}] took [{2}] milliseconds to execute",
   eventArgs.Method.DeclaringType.Name,
   eventArgs.Method.Name,
   timeSpent);
   }
   
   if (timeSpent > Threshold)
   {
   Log.WarnFormat(
   "Method [{0}{1}] was expected to finish within [{2}] milliseconds, but took [{3}] instead!",
   eventArgs.Method.DeclaringType.Name,
   eventArgs.Method.Name,
   Threshold,
   timeSpent);
   }
 }
     // Record time spent executing the method
     public override void OnInvocation(MethodInvocationEventArgs eventArgs)
     {
         var sw = Stopwatch.StartNew();
         eventArgs.Proceed();
         sw.Stop();
         var timeSpent = sw.ElapsedMilliseconds;
  
         if (LogDebug)
         {
             Log.DebugFormat(
                 "Method [{0}{1}] took [{2}] milliseconds to execute",
                 eventArgs.Method.DeclaringType.Name,
                 eventArgs.Method.Name,
                 timeSpent);
         }
  
         if (timeSpent > Threshold)
         {
             Log.WarnFormat(
                 "Method [{0}{1}] was expected to finish within [{2}] milliseconds, but took [{3}] instead!",
                 eventArgs.Method.DeclaringType.Name,
                 eventArgs.Method.Name,
                 Threshold,
                 timeSpent);
        }
 }
Exemplo n.º 3
0
        /*public static void applyPatch(object[] readOnlyArguments, MethodExecutionEventArgs context)
        {
            var stringToFind = "QQQQ";
            if (readOnlyArguments != null)
                for (int i = 0; i < readOnlyArguments.Length; i++)
                {
                    var argument = readOnlyArguments[i];
                    if (argument is string && argument.ToString() == stringToFind)
                    {
                        Trace.WriteLine("****************************************************************************");
                        Trace.WriteLine("     - Patch point     ");
                        Trace.WriteLine("****************************************************************************");
                        var writeableArguments = context.GetWritableArgumentArray();
                        writeableArguments[i] = " *** PATCHED QQQQ ";                        

                        // now lets see if the patch values are there

                        var postPatchReadOnly = context.GetReadOnlyArgumentArray();
                        if (postPatchReadOnly[i].ToString() == " *** PATCHED QQQQ ")
                            Trace.WriteLine("!!!!!!! Yap Patch is there ");

                    }
                }

        }*/


        public static void applyPatch(MethodInvocationEventArgs context)
        {
            var stringToFind = "<script>";
            var argumentArray = context.GetArgumentArray();
            if (argumentArray != null)
                for (int i = 0; i < argumentArray.Length; i++)
                {
                    var argument = argumentArray[i];
                    if (argument is string && argument.ToString().IndexOf(stringToFind) > -1)
                    {
                        Trace.WriteLine("****************************************************************************");
                        Trace.WriteLine("     - Patch point     ");
                        Trace.WriteLine("****************************************************************************");

                        argumentArray[i] = htmlEncode(argumentArray[i].ToString());//.ToString().Replace(stringToFind, "script]");

                        /*// now lets see if the patch values are there

                        var postPatchReadOnly = context.GetReadOnlyArgumentArray();
                        if (postPatchReadOnly[i].ToString() == " *** PATCHED QQQQ ")
                            Trace.WriteLine("!!!!!!! Yap Patch is there ");
                        */
                    }
                }

        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="eventArgs"></param>
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            object objRtn    = null;
            string controlId = "";

            //check for caching
            string cacheKey = CreateKeyNameAndSetMethodParameters(eventArgs, ref controlId);

            objRtn = GetFromCache(cacheKey);

            if (objRtn == null)
            {
                //creates key, and get _controlId id and _time
                objRtn = eventArgs.Delegate.DynamicInvoke(eventArgs.GetArguments());

                if (!String.IsNullOrEmpty(controlId))   //controlId is required to cache
                {
                    if (_time != "")                    //time is required to cache
                    {
                        AddToCache(cacheKey, controlId, _time, objRtn);
                    }
                }
            }

            eventArgs.ReturnValue = objRtn;
        }
Exemplo n.º 5
0
 public override void OnInvocation(MethodInvocationEventArgs eventArgs)
 {
     ReaderWriterLock rwLock = null; // GetLock(); - get the right lock
     rwLock.AcquireReaderLock(10000);
     try { eventArgs.Proceed(); }
     finally { rwLock.ReleaseReaderLock(); }
 }
        /*public static void applyPatch(object[] readOnlyArguments, MethodExecutionEventArgs context)
         * {
         *  var stringToFind = "QQQQ";
         *  if (readOnlyArguments != null)
         *      for (int i = 0; i < readOnlyArguments.Length; i++)
         *      {
         *          var argument = readOnlyArguments[i];
         *          if (argument is string && argument.ToString() == stringToFind)
         *          {
         *              Trace.WriteLine("****************************************************************************");
         *              Trace.WriteLine("     - Patch point     ");
         *              Trace.WriteLine("****************************************************************************");
         *              var writeableArguments = context.GetWritableArgumentArray();
         *              writeableArguments[i] = " *** PATCHED QQQQ ";
         *
         *              // now lets see if the patch values are there
         *
         *              var postPatchReadOnly = context.GetReadOnlyArgumentArray();
         *              if (postPatchReadOnly[i].ToString() == " *** PATCHED QQQQ ")
         *                  Trace.WriteLine("!!!!!!! Yap Patch is there ");
         *
         *          }
         *      }
         *
         * }*/


        public static void applyPatch(MethodInvocationEventArgs context)
        {
            var stringToFind  = "<script>";
            var argumentArray = context.GetArgumentArray();

            if (argumentArray != null)
            {
                for (int i = 0; i < argumentArray.Length; i++)
                {
                    var argument = argumentArray[i];
                    if (argument is string && argument.ToString().IndexOf(stringToFind) > -1)
                    {
                        Trace.WriteLine("****************************************************************************");
                        Trace.WriteLine("     - Patch point     ");
                        Trace.WriteLine("****************************************************************************");

                        argumentArray[i] = htmlEncode(argumentArray[i].ToString());//.ToString().Replace(stringToFind, "script]");

                        /*// now lets see if the patch values are there
                         *
                         * var postPatchReadOnly = context.GetReadOnlyArgumentArray();
                         * if (postPatchReadOnly[i].ToString() == " *** PATCHED QQQQ ")
                         *  Trace.WriteLine("!!!!!!! Yap Patch is there ");
                         */
                    }
                }
            }
        }
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            // Compute the cache key.
            string key = Formatter.GetMethodFormatStrings(eventArgs.Delegate.Method)
                         .Format(eventArgs.Delegate.Target,
                                 eventArgs.Delegate.Method,
                                 eventArgs.GetArgumentArray());

            object value;

            if (!cache.TryGetValue(key, out value))
            {
                lock (this)
                {
                    if (!cache.TryGetValue(key, out value))
                    {
                        eventArgs.Proceed();
                        value = eventArgs.ReturnValue;
                        cache.Add(key, value);
                        return;
                    }
                }
            }

            eventArgs.ReturnValue = value;
        }
Exemplo n.º 8
0
            // intercept the method invocation
            public override void OnInvocation(MethodInvocationEventArgs eventArgs)
            {
                // get the arguments that were passed to the method
                object[] args = eventArgs.GetArgumentArray();

                // start building a key based on the method name
                // because it wouldn't help to return the same value
                // every time "lulu" was passed to any method
                StringBuilder keyBuilder = new StringBuilder(eventArgs.Delegate.Method.Name);

                // append the hashcode of each arg to the key
                // this limits us to value types (and strings)
                // i need a better way to do this (and preferably
                // a faster one)
                for (int i = 0; i < args.Length; i++)
                    keyBuilder.Append(args[i].GetHashCode());

                string key = keyBuilder.ToString();

                // if the key doesn't exist, invoke the original method
                // passing the original arguments and store the result
                if (!memos.ContainsKey(key))
                    memos[key] = eventArgs.Delegate.DynamicInvoke(args);

                // return the memo
                eventArgs.ReturnValue = memos[key];
            }
Exemplo n.º 9
0
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            DispatcherObject dispatcherObject = (eventArgs.Delegate.Target ?? eventArgs.GetArgumentArray()[0]) as DispatcherObject;

            if (dispatcherObject != null)
            {
                if (dispatcherObject.CheckAccess())
                {
                    // We are already in the GUI thread. Proceed.
                    eventArgs.Proceed();
                }
                else
                {
                    if (Async)
                    {
                        // Invoke the target method asynchronously (don't wait).
                        dispatcherObject.Dispatcher.BeginInvoke(Priority,
                                                                new Action(() => eventArgs.Proceed()));
                    }
                    else
                    {
                        // Invoke the target method synchronously.
                        dispatcherObject.Dispatcher.Invoke(
                            Priority,
                            new Action(() => eventArgs.Proceed()));
                    }
                }
            }
            else
            {
                eventArgs.Proceed();
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Creates Key name based on the method name and it's parameters
        /// </summary>
        /// <param name="method"></param>
        /// <param name="controlId"></param>
        /// <returns></returns>
        private void GetSiteId(MethodInvocationEventArgs method)
        {
            int no = 0;

            _siteId    = 0;
            _accessKey = "";

            ParameterInfo[] prmsInfo = method.Delegate.Method.GetParameters();
            if (prmsInfo != null && prmsInfo.Length > 0)
            {
                object[] objParamsValues = method.GetArguments();
                foreach (ParameterInfo prm in prmsInfo)
                {
                    if (prm.Name.ToLower() == "siteid" && objParamsValues[no].ToString().Trim() != "")
                    {
                        _siteId = Convert.ToInt64(objParamsValues[no].ToString());
                    }
                    else if (prm.Name.ToLower() == "accesskey" && objParamsValues[no].ToString().Trim() != "")
                    {
                        _accessKey = objParamsValues[no].ToString();
                    }
                    no++;
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="eventArgs"></param>
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            GetSiteId(eventArgs);
            bool hasRight = false;


            if (_siteId != 0)
            {
                switch (_securityType.ToString().ToLower())
                {
                case "domain":
                    hasRight = AWAPI_BusinessLibrary.library.SecurityLibrary.IsValidReferrer(_siteId);
                    break;

                case "accesskey":
                    hasRight = AWAPI_BusinessLibrary.library.SecurityLibrary.IsValidAccessKey(_siteId, _accessKey);
                    break;
                }
            }

            if (!hasRight)
            {
                throw new System.ServiceModel.FaultException("You don't have permission.");
            }
            else
            {
                //eventArgs.Proceed();
                object objRtn = eventArgs.Delegate.DynamicInvoke(eventArgs.GetArguments());
                eventArgs.ReturnValue = objRtn;
            }
        }
Exemplo n.º 12
0
        /// <summary>
        ///   The method that wraps the decorated method.
        ///   This method performs the actual 
        /// </summary>
        /// <param name="eventArgs"></param>
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            InitializeCache();

            var cacheOptions = GetCacheOptions(eventArgs);
            if (cacheOptions != null)
            {
                ExcludeCacheOptionsParam(eventArgs);
            }

            var key = _formatStrings.Format(eventArgs.Instance, eventArgs.Method, GetFilteredArgumentArray(eventArgs));

            if (cacheOptions == CacheOptions.Refresh)
            {
                RemoveFromCache(key);
            }

            // Test whether the cache contains the current method call.
            var value = GetValueFromCacheKey(key, cacheOptions);

            if (value == null)
            {
                eventArgs.Proceed();
                value = eventArgs.ReturnValue;
                AddToCache(key, value, CacheItemPriority.Normal, null, new TimeSpan(0, CacheMinutes, 0), cacheOptions);
            }

            eventArgs.ReturnValue = value;
        }
Exemplo n.º 13
0
 public override void OnInvocation(MethodInvocationEventArgs eventArgs)
 {
     if (_conditions[0] == "Bob")     // do whatever check you want here
     {
         eventArgs.Proceed();
     }
 }
 private static void LogTransactionDetails(MethodInvocationEventArgs eventArgs, Account account)
 {
     Console.WriteLine(string.Format(
         "{2} ${0} on account {1}.",
         eventArgs.GetArgumentArray().First(),
         account.Number,
         eventArgs.Method.Name));
 }
 private static void LogTransactionFinished(MethodInvocationEventArgs eventArgs, Account account)
 {
     Console.WriteLine(string.Format(
        "Finished {2} transaction on account {0}. Balance after the operation is ${1}.",
        account.Number,
        account.CurrentBalance,
        eventArgs.Method.Name));
 }
 private static void LogTransactionStarting(MethodInvocationEventArgs eventArgs, Account account)
 {
     Console.WriteLine(string.Format(
         "New {2} transaction on account {0}. Balance before operation is ${1}.",
         account.Number,
         account.CurrentBalance,
         eventArgs.Method.Name));
 }
Exemplo n.º 17
0
        private void InvokeUsingBackgroundThread(MethodInvocationEventArgs eventArgs)
        {
            var thread = new Thread(eventArgs.Proceed)
                {
                   IsBackground = true
                };

            thread.Start();
        }
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            Account account = (Account)eventArgs.Instance;

            LogTransactionStarting(eventArgs, account);
            LogTransactionDetails(eventArgs, account);
            eventArgs.Proceed();
            LogTransactionFinished(eventArgs, account);
        }
Exemplo n.º 19
0
 public override void OnInvocation(MethodInvocationEventArgs eventArgs)
 {
     try
     {
         eventArgs.Proceed();
     }
     catch (Exception ex)
     {
         // log exception here
     }
 }
Exemplo n.º 20
0
/*
 *      public override bool CompileTimeValidate(MethodBase method)
 *      {
 *          if (!method.DeclaringType.Implements(typeof (ISynchronizeInvoke)))
 *          {
 *              throw new ArgumentOutOfRangeException(
 *                  string.Format(
 *                      "InvokeRequired attribute used in class {0} that does not implement ISynchronizeInvoke",
 *                      method.DeclaringType.Name));
 *          }
 *          return base.CompileTimeValidate(method);
 *      }
 */

        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            var synchronizeObject = SynchronizeObject ?? eventArgs.Instance as ISynchronizeInvoke;

            if (synchronizeObject == null || !synchronizeObject.InvokeRequired)
            {
                eventArgs.Proceed();
            }
            else
            {
                synchronizeObject.Invoke(eventArgs.Delegate, eventArgs.GetArgumentArray());
            }
        }
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            var target = eventArgs.Instance as ISynchronizeInvoke;

            if (!target.InvokeRequired)
            {
                eventArgs.Proceed();
            }
            else
            {
                target.Invoke(eventArgs.Delegate, eventArgs.GetArgumentArray());
            }
        }
Exemplo n.º 22
0
 public static void applyPatch_encodeAllStringParameters(MethodInvocationEventArgs context)
 {
     var argumentArray = context.GetArgumentArray();
     if (argumentArray != null)
         for (int i = 0; i < argumentArray.Length; i++)
         {
             var argument = argumentArray[i];
             if (argument is string)
             {
                 Trace.WriteLine("**** Patching value:" + argumentArray[i]);
                 argumentArray[i] = htmlEncode(argumentArray[i].ToString());
             }
         }
 }    
        /// <summary>
        /// Method called instead of the intercepted method.
        /// </summary>
        /// <param name="context">Event arguments specifying which method
        /// is being executed and which are its arguments. The implementation
        /// should set the return value and ouput arguments.</param>
        public override void OnInvocation(MethodInvocationEventArgs context)
        {
            string methodName = "";

            try
            {
                methodName = this.formatStrings.Format((context.Delegate.Target) ?? null,
                                                       context.Delegate.Method,
                                                       context.GetArgumentArray()); // +

                /*    ( context.Delegate.Method == this.method
                 *        ? ""
                 *        : " [overriden by class " + context.Delegate.Method.DeclaringType.FullName + "]" );*/

                //Patches.applyPatch(context);
                //Patches.applyPatch_encodeAllStringParameters(context);

                /*var arguments = context.GetArgumentArray();
                 *
                 * if (arguments != null)
                 * {
                 * //                    Trace.Write("There are " + arguments.Length + " arguments");
                 *  if (arguments[0].ToString() == "Mr. ")
                 *      arguments[0] = " MISS. ";
                 * }*/

                Trace.Write(this.prefix + "__Entering " + methodName);
            }
            catch (Exception ex)
            {
                Trace.Write("Error: " + ex.Message);
            }
            try
            {
                context.Proceed();
            }
            catch (Exception e)
            {
                Trace.Unindent();

                //    Trace.TraceWarning(
                //        Formatter.FormatString( "{0}Leaving {{{1}}} with exception {2}: {{{3}}}.",
                //                                this.prefix, methodName, e.GetType().Name, e.Message ) );
                throw;
            }

            Trace.Write(
                this.prefix + "Leaving " + methodName + (this.isVoid ? "" : Formatter.FormatString(" : {{{0}}}.", context.ReturnValue)));
        }
Exemplo n.º 24
0
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            Action proceedAction = eventArgs.Proceed;
            IAsyncResult result = proceedAction.BeginInvoke(
                null,
                null);

            bool completed = result.AsyncWaitHandle.WaitOne(this.TimeoutMs);

            if (!completed)
            {
                throw new TimeoutException();
            }

            proceedAction.EndInvoke(result);
        }
Exemplo n.º 25
0
    public override void OnInvocation(MethodInvocationEventArgs e)
    {
        if (e.Delegate.Method.ReturnParameter.ParameterType == typeof(void))
        {
            // we're in the setter
            IChangeTrackable target = e.Delegate.Target as IChangeTrackable;

            // Implement some logic to retrieve the current value of
            // the property
            if (currentValue != e.GetArgumentArray()[0])
            {
                target.Status = Status.Dirty;
            }
            base.OnInvocation(e);
        }
    }
Exemplo n.º 26
0
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            switch (this.InvocationOption)
            {
                case AsynchronousInvocationOption.BackgroundThread:
                    this.InvokeUsingBackgroundThread(eventArgs);
                    break;

                case AsynchronousInvocationOption.Delegate:
                    this.InvokeUsingDelegate(eventArgs);
                    break;

                default:
                    this.InvokeUsingThreadPool(eventArgs);
                    break;
            }
        }
Exemplo n.º 27
0
        public static void applyPatch_encodeAllStringParameters(MethodInvocationEventArgs context)
        {
            var argumentArray = context.GetArgumentArray();

            if (argumentArray != null)
            {
                for (int i = 0; i < argumentArray.Length; i++)
                {
                    var argument = argumentArray[i];
                    if (argument is string)
                    {
                        Trace.WriteLine("**** Patching value:" + argumentArray[i]);
                        argumentArray[i] = htmlEncode(argumentArray[i].ToString());
                    }
                }
            }
        }
Exemplo n.º 28
0
 public override void OnInvocation(MethodInvocationEventArgs eventArgs)
 {
     object[]        arguments  = eventArgs.GetArgumentArray();
     ParameterInfo[] parameters = eventArgs.Delegate.Method.GetParameters();
     for (int i = 0; i < arguments.Length; i++)
     {
         if (arguments[i] != null)
         {
             continue;
         }
         foreach (CanBeNullAttribute attribute in parameters[i].GetCustomAttributes(typeof(CanBeNullAttribute), true))
         {
             if (!attribute.AllowNull)
             {
                 throw new ArgumentNullException(parameters[i].Name);
             }
         }
     }
     base.OnInvocation(eventArgs);
 }
Exemplo n.º 29
0
        /// <summary>
        /// Creates Key name based on the method name and it's parameters
        /// </summary>
        /// <param name="method"></param>
        /// <param name="controlId"></param>
        /// <returns></returns>
        private string CreateKeyNameAndSetMethodParameters(MethodInvocationEventArgs method, ref string controlId)
        {
            string prms = "";

            int no = 0;

            _time     = "";
            controlId = "";

            ParameterInfo[] prmsInfo = method.Delegate.Method.GetParameters();
            if (prmsInfo != null && prmsInfo.Length > 0)
            {
                object[] objParamsValues = method.GetArguments();
                foreach (ParameterInfo prm in prmsInfo)
                {
                    if (objParamsValues[no] == null)
                    {
                        prms += "null|";
                    }
                    else
                    {
                        prms += objParamsValues[no].ToString() + "|";

                        if (prm.Name.ToLower() == "cacheduration" && objParamsValues[no].ToString().Trim() != "")
                        {
                            _time = objParamsValues[no].ToString();
                        }
                        else if (prm.Name.ToLower() == "controlid" && objParamsValues[no].ToString().Trim() != "")
                        {
                            controlId = objParamsValues[no].ToString();
                        }
                    }
                    no++;
                }
            }
            return(method.Delegate.Method.Name + "|" + prms);
        }
Exemplo n.º 30
0
 public override void OnInvocation(MethodInvocationEventArgs eventArgs)
 {
     ThreadPool.QueueUserWorkItem(delegate { eventArgs.Proceed(); });
 }
Exemplo n.º 31
0
        /// <summary>
        /// Method called instead of the intercepted method.
        /// </summary>
        /// <param name="context">Event arguments specifying which method
        /// is being executed and which are its arguments. The implementation
        /// should set the return value and ouput arguments.</param>
        public override void OnInvocation( MethodInvocationEventArgs context )
        {                     
            string methodName = "";
            try
            {
                methodName = this.formatStrings.Format((context.Delegate.Target) ?? null,
                                           context.Delegate.Method,
                                           context.GetArgumentArray()); // +  
                /*    ( context.Delegate.Method == this.method
                          ? ""
                          : " [overriden by class " + context.Delegate.Method.DeclaringType.FullName + "]" );*/

                //Patches.applyPatch(context);
                //Patches.applyPatch_encodeAllStringParameters(context);

                /*var arguments = context.GetArgumentArray();

                if (arguments != null)
                {
//                    Trace.Write("There are " + arguments.Length + " arguments");
                    if (arguments[0].ToString() == "Mr. ")
                        arguments[0] = " MISS. ";
                }*/

                Trace.Write(this.prefix + "__Entering " + methodName);
            }
            catch (Exception ex)
            {
                Trace.Write("Error: " + ex.Message);
            }
            try
            {
                context.Proceed();
            }
            catch ( Exception e )
            {
                Trace.Unindent();

            //    Trace.TraceWarning(
            //        Formatter.FormatString( "{0}Leaving {{{1}}} with exception {2}: {{{3}}}.",
            //                                this.prefix, methodName, e.GetType().Name, e.Message ) );
                throw;
            }
            
            Trace.Write(
                this.prefix + "Leaving " + methodName + ( this.isVoid ? "" : Formatter.FormatString( " : {{{0}}}.", context.ReturnValue ) ) );
        }
Exemplo n.º 32
0
 public override void OnInvocation(MethodInvocationEventArgs context)
 {
     Console.WriteLine("Calling {0}", context.Delegate.Method);
     context.Proceed();
 }
Exemplo n.º 33
0
        private object[] GetFilteredArgumentArray(MethodInvocationEventArgs e)
        {
            if (ExcludedParams == null)
            {
                return e.GetArgumentArray();
            }

            var args = e.GetArgumentArray();

            // Replace objects at specified indexes in the array with null-value
            return args.Select((arg, index) => ExcludedParams.Contains(index) ? null : arg).ToArray();
        }
Exemplo n.º 34
0
 public override void OnInvocation(MethodInvocationEventArgs eventArgs)
 {
     ThreadPool.QueueUserWorkItem(delegate { eventArgs.Proceed(); });
 }
Exemplo n.º 35
0
 private void InvokeUsingThreadPool(MethodInvocationEventArgs eventArgs)
 {
     ThreadPool.QueueUserWorkItem(delegate { eventArgs.Proceed(); });
 }
Exemplo n.º 36
0
 private void InvokeUsingDelegate(MethodInvocationEventArgs eventArgs)
 {
     var proceed = new Action(eventArgs.Proceed);
     proceed.BeginInvoke(proceed.EndInvoke, proceed);
 }
Exemplo n.º 37
0
        private void ExcludeCacheOptionsParam(MethodInvocationEventArgs eventArgs)
        {
            var index = eventArgs.Method.GetParameters().Length - 1;
            if (ExcludedParams != null && ExcludedParams.Contains(index))
            {
                return;
            }

            ExcludedParams = ExcludedParams != null ? ExcludedParams.Concat(new[] { index }).ToArray() : new[] { index };
        }
Exemplo n.º 38
0
 public override void OnInvocation(MethodInvocationEventArgs eventArgs)
 {
     Console.WriteLine("Hello, i'm modified method");
     base.OnInvocation(eventArgs);
 }
Exemplo n.º 39
0
        private static CacheOptions? GetCacheOptions(MethodInvocationEventArgs e)
        {
            var args = e.Method.GetParameters();
            if (args.Length == 0)
            {
                return null;
            }

            if (args[args.Length - 1].ParameterType == typeof(CacheOptions))
            {
                return (CacheOptions)e.GetArgumentArray()[args.Length - 1];
            }

            return null;
        }
Exemplo n.º 40
0
        /// <summary>
        /// Returns the full cache key based on the previously built cache key and the current execution
        /// context.
        /// </summary>
        /// <param name="eventArgs">
        /// Arguments of the method that is being invoked. This will be used to gen the cache key
        /// </param>
        /// <returns>
        /// If the user has specified a parameter to vary by, the value of that parameter is appended
        /// to the previously built cache key and returned. Otherwise, just the previously built key is 
        /// returned.
        /// </returns>
        private CacheKey GetCacheKey(MethodInvocationEventArgs eventArgs)
        {
            var fullCacheKey = new StringBuilder(this.cacheKey);

            object[] arguments = eventArgs.GetArgumentArray();

            if (arguments != null)
            {
                foreach (object argument in arguments)
                {
                    fullCacheKey.Append("-");

                    if (argument is ICacheable)
                    {
                        var cacheable = argument as ICacheable;
                        fullCacheKey.Append(this.ComputeCacheKeyHash(cacheable.GenerateCacheKey()));
                    }
                    else
                    {
                        fullCacheKey.Append(this.ComputeCacheKeyHash(argument.ToString()));
                    }
                }
            }

            return new CacheKey(this.Cache, fullCacheKey.ToString());
        }
Exemplo n.º 41
0
        /// <summary>
        /// Performs the caching / retrievel of the object.
        /// </summary>
        /// <param name="eventArgs">
        /// The event args.
        /// </param>
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            // Get the full cache key
            CacheKey fullCacheKey = this.GetCacheKey(eventArgs);

            // Check to see if the cache contains the target item
            eventArgs.ReturnValue = CachingService[fullCacheKey];

            if (eventArgs.ReturnValue == null)
            {
                lock (LockableObjectProvider.GetLockableObject(fullCacheKey))
                {
                    // Check again to see if the cache contains the target item (double check lock pattern)
                    eventArgs.ReturnValue = cachingService[fullCacheKey];

                    // If we still don't have a return value, proceed with the original method to populate it
                    if (eventArgs.ReturnValue == null)
                    {
                        eventArgs.Proceed();

                        CachingService.Add(fullCacheKey, eventArgs.ReturnValue ?? NullObject);
                    }
                }
            }

            if (eventArgs.ReturnValue is string && (string)eventArgs.ReturnValue == NullObject)
            {
                eventArgs.ReturnValue = null;
            }
        }
Exemplo n.º 42
0
 public override void OnInvocation(MethodInvocationEventArgs context)
 {
     Console.WriteLine("Calling {0}", context.Delegate.Method);
     context.Proceed();
 }