/// <summary>
        ///   Method invoked <i>instead</i> of the method to which the aspect has been applied.
        /// </summary>
        /// <param name="args">Advice arguments.</param>
        public override async Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            if (!ShouldIntercept(args, out var auditLog, out var auditLogAction))
            {
                await args.ProceedAsync();

                return;
            }

            var stopwatch = Stopwatch.StartNew();

            try
            {
                await args.ProceedAsync();
            }
            catch (Exception ex)
            {
                auditLog.Exceptions.Add(ex);
                throw;
            }
            finally
            {
                stopwatch.Stop();
                auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
                auditLog.Actions.Add(auditLogAction);
            }
        }
        public override async Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            if (args.Instance is ICacheAware cacheAware && !cacheAware.CacheEnabled)
            {
                await args.ProceedAsync();

                return;
            }

            var key = GetKey(args.Method as MethodInfo, args.Arguments);

            using (var connection = ConnectionMultiplexer.Connect(redisServer.Value))
            {
                var db         = connection.GetDatabase();
                var redisValue = await db.StringGetAsync(key);

                if (redisValue.IsNullOrEmpty)
                {
                    await args.ProceedAsync();

                    db.StringSet(key, JsonConvert.SerializeObject(args.ReturnValue), Expiry);
                }
                else
                {
                    args.ReturnValue = JsonConvert.DeserializeObject(redisValue.ToString(), (args.Method as MethodInfo).ReturnType.GenericTypeArguments[0]);
                }
            }
        }
Beispiel #3
0
        public override async Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            int retriesCounter = 0;
            await args.ProceedAsync();

            Debug.WriteLine(
                "Exception during attempt {0} of calling method {1}.{2}:", retriesCounter, args.Method.DeclaringType, args.Method.Name);

            return;

            while (true)
            {
                try
                {
                }
                catch (Exception e)
                {
                    retriesCounter++;
                    if (retriesCounter > this.MaxRetries)
                    {
                        throw;
                    }

                    Console.WriteLine(
                        "Exception during attempt {0} of calling method {1}.{2}: {3}",
                        retriesCounter, args.Method.DeclaringType, args.Method.Name, e.Message);
                }
            }
        }
Beispiel #4
0
        public override async Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            var i = GetArgumentIndex(args);

            if (!i.HasValue)
            {
                await args.ProceedAsync();

                return;
            }

            using (IDbConnection db = new MySqlConnection(ConnectionString))
            {
                args.Arguments.SetArgument(i.Value, db);
                await args.ProceedAsync();
            }
        }
Beispiel #5
0
        public override async Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            args.Arguments[0] = defaultInputParameter;
            await args.ProceedAsync();

            //or you can use await base.OnInvokeAsync(args);
            args.SetReturnValue(defaultReturnParameter);
        }
Beispiel #6
0
        public override async Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            var inputArg = args.Arguments.First();

            args.Arguments[0] = defaultInputParameter;
            await args.ProceedAsync();

            //or you can use await base.OnInvokeAsync(args);
        }
        public override Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            var taskcompletionSource = new TaskCompletionSource <object>();

            Forms.Device.BeginInvokeOnMainThread(async() =>
            {
                await args.ProceedAsync();
                taskcompletionSource.SetResult(new object());
            });
            return(taskcompletionSource.Task);
        }
Beispiel #8
0
        public async override Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
            {
                IsolationLevel = IsolationLevel
            }, TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    await args.ProceedAsync();

                    scope.Complete();
                }
                finally
                {
                    scope.Dispose();
                }
            }
        }
Beispiel #9
0
        /// <summary>
        ///   Method invoked <i>instead</i> of the method to which the aspect has been applied.
        /// </summary>
        /// <param name="args">Advice arguments.</param>
        public override async Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            var auditingManager = CreateAuditingManager();

            using (var scope = auditingManager.BeginScope())
            {
                try
                {
                    await args.ProceedAsync();

                    var exs = auditingManager.Current !.Log.Exceptions;
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    await scope.SaveAsync();
                }
            }
        }
        public override async Task OnInvokeAsync(MethodInterceptionArgs args)
        {
            for (var i = 0; ; i++)
            {
                try
                {
                    // Invoke the intercepted method.
                    await args.ProceedAsync();

                    // If we get here, it means the execution was successful.
                    return;
                }
                catch (Exception e)
                {
                    // The intercepted method threw an exception. Figure out if we can retry the method.

                    if (CanRetry(i, e))
                    {
                        // Yes, we can retry. Write some message and wait a bit.

                        Console.WriteLine(
                            "Method failed with exception {0}. Sleeping {1} s and retrying. This was our attempt #{2}.",
                            e.GetType().Namespace, Delay, i + 1);

                        if (Delay > 0)
                        {
                            await Task.Delay(TimeSpan.FromSeconds(Delay));
                        }

                        // Continue to the next iteration.
                    }
                    else
                    {
                        // No, we cannot retry. Retry the exception.
                        throw;
                    }
                }
            }
        }
Beispiel #11
0
 public override Task OnInvokeAsync(MethodInterceptionArgs args)
 {
     return(Task.Run(async() => await args.ProceedAsync()));
 }
Beispiel #12
0
 public virtual async Task OnInvokeAsync(MethodInterceptionArgs args)
 {
     await args.ProceedAsync();
 }