Example #1
0
 /// <inheritdoc />
 public void AfterInvoke(InvocationContext invocationContext, object methodResult)
 {
     // Save the result to the MemoryCahe with an expiration time if availble
     if (invocationContext.GetAttributeFromMethod <CacheAttribute>() != null &&
         invocationContext.GetAttributeFromMethod <CacheAttribute>().MillisecondsToExpire > 0)
     {
         this.memoryCache.Set(invocationContext.GetExecutingMethodName(), methodResult, TimeSpan.FromMilliseconds(invocationContext.GetAttributeFromMethod <CacheAttribute>().MillisecondsToExpire));
     }
     else
     {
         // Save the result to the MemoryCache without any expiration time
         this.memoryCache.Set(invocationContext.GetExecutingMethodName(), methodResult);
     }
 }
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            _memoryCacheAttribute = invocationContext.GetAttributeFromMethod<MemoryCacheAttribute>();
            var cacheKey = _memoryCacheAttribute.CacheKey;
            var cacheConfig = _cacheConfigFactory.Config[cacheKey];
            if (cacheConfig.Enabled)
            {
                if (!string.IsNullOrWhiteSpace(_memoryCacheAttribute.CacheKeyParameterSubstitutions))
                {
                    List<object> paramValues = new List<object>();
                    string[] paramNames = _memoryCacheAttribute.CacheKeyParameterSubstitutions.Split(",", StringSplitOptions.RemoveEmptyEntries);
                    foreach (string paramName in paramNames)
                    {
                        paramValues.Add(invocationContext.GetParameterValue(paramName.Trim()));

                    }
                    cacheKey = string.Format(cacheConfig.CacheKey.ToString(), paramValues.ToArray());

                }
                CacheKey = cacheKey;

                if (_memoryCacheAttribute.RefreshCache)
                {
                    _memoryCacheService.Remove(CacheKey);
                }

                var cacheReturnObj = RetrieveCacheEntry(invocationContext, CacheKey);
                if (cacheReturnObj != null && cacheReturnObj.CacheEntry != null)
                {
                    var returnType = invocationContext.GetMethodReturnType();
                    var isAsyncMethod = returnType != null && returnType.IsGenericType &&
                        returnType.GetGenericTypeDefinition() == typeof(Task<>);
                    if (isAsyncMethod)
                    {
                        invocationContext.OverrideAsyncMethodReturnValue(cacheReturnObj.CacheEntry);
                    }
                    else
                    {
                        invocationContext.OverrideMethodReturnValue(cacheReturnObj.CacheEntry);
                    }

                    invocationContext.TempData[CacheConstants.LOGGING_CACHE_INVOCATIONCONETXT] = string.Format(
                        "{0}{1}{2}", CacheConstants.LOGGING_CACHE_HIT, CacheConstants.LOGGING_CACHE_DELIMITER, cacheKey);
                    invocationContext.BypassInvocation();
                }
                else
                {
                    invocationContext.TempData[CacheConstants.LOGGING_CACHE_INVOCATIONCONETXT] = string.Format(
                           "{0}{1}{2}", CacheConstants.LOGGING_CACHE_MISS, CacheConstants.LOGGING_CACHE_DELIMITER, cacheKey);
                }

            }
        }
Example #3
0
        /// <inheritdoc />
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            // Create a logger based on the class type that owns the executing method
            this.logger = this.loggerFactory.CreateLogger(invocationContext.GetOwningType());

            // Get the Logging Attribute
            this.loggingAttribute = invocationContext.GetAttributeFromMethod <LogAttribute>();

            // Get the Logging Level
            var loggingLevel = this.loggingAttribute.LoggingLevel;

            // Log the method being executed
            this.logger.Log(loggingLevel, $"{invocationContext.GetOwningType()}: Method executing: {invocationContext.GetExecutingMethodName()}");
        }
Example #4
0
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            this._validateAttribute = invocationContext.GetAttributeFromMethod <ValidateAttribute>();

            if (typeof(IValidation).IsAssignableFrom(_validateAttribute._validatorType) == false)
            {
                throw new Exception("Wrong validation manager");
            }

            var validator = (IValidation)ActivatorUtilities.CreateInstance(_serviceProvider, _validateAttribute._validatorType);

            int entityPosition = invocationContext.GetParameterPosition(_validateAttribute._entityType);
            var entity         = invocationContext.GetParameterValue(entityPosition);

            if (entity != null)
            {
                validator.Validator(entity);
            }
        }
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            // Initialize fresh Log object for each Layer copying from Global log object

            _logEntry = invocationContext.ServiceProvider.GetRequiredService <TransactionLogEntry>();
            IBaseHandler targetObject = invocationContext.Invocation.InvocationTarget as IBaseHandler;

            if (targetObject != null)
            {
                var targetLogEntry = CommonLogUtility.populateTransaction(_logEntry);
                targetObject.LockObject = new object();
                targetObject.LogEntry   = targetLogEntry;
            }
            _stopWatch.Reset();
            _stopWatch.Start();

            // Get the logging attribute
            this.loggingAttribute = invocationContext.GetAttributeFromMethod <LogMethodAttribute>();
            // To DO: Customized with your code before finction call here..
        }
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            this._cacheAttribute = invocationContext.GetAttributeFromMethod <CacheAttribute>();

            if (typeof(ICacheManager).IsAssignableFrom(_cacheAttribute._cacheType) == false)
            {
                throw new Exception("Wrong cache manager");
            }

            cacheManager = (ICacheManager)ActivatorUtilities.CreateInstance(_serviceProvider, _cacheAttribute._cacheType);

            string key = GetMethodKey(invocationContext);


            cacheManager.TryGet(key, out var result);
            if (result != null)
            {
                invocationContext.OverrideMethodReturnValue(result);
                invocationContext.BypassInvocation();
            }
        }
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            this._logAttribute = invocationContext.GetAttributeFromMethod <LogAttribute>();

            if (typeof(ILoggerService).IsAssignableFrom(_logAttribute._loggerServiceType) == false)
            {
                throw new Exception("Wrong log manager");
            }

            logManager = (ILoggerService)ActivatorUtilities.CreateInstance(_serviceProvider, _logAttribute._loggerServiceType);

            LogDetail _log = new LogDetail()
            {
                DateTime   = DateTime.Now,
                FullName   = invocationContext.GetInvocation().TargetType.FullName,
                MethodName = invocationContext.GetExecutingMethodInfo().Name,
                Type       = _logAttribute._logType.ToString()
                             // Arguments = GetArguments(invocationContext)
            };

            logManager.Logging(_log);
        }
Example #8
0
        public void AfterInvoke(InvocationContext invocationContext, object methodResult)
        {
            try
            {
                var attribute = invocationContext.GetAttributeFromMethod <CacheAttribute>();

                var argsDictionary = new Dictionary <string, object>();
                var args           = invocationContext.GetExecutingMethodInfo().GetParameters();
                for (var i = 0; i < args.Length; i++)
                {
                    var argumentValue = invocationContext.GetParameterValue(i);
                    var argumentName  = args[i].Name;
                    argsDictionary.Add(argumentName, argumentValue);
                }

                var cacheKey = invocationContext.GetExecutingMethodInfo().GetParameters().Length > 0
                    ? CacheKeyGenerator.GenerateCacheKey(invocationContext.GetExecutingMethodInfo(), argsDictionary)
                    : invocationContext.GetExecutingMethodInfo().Name;

                var cachingDuration = new TimeSpan(attribute.Day, attribute.Hour, attribute.Minute, attribute.Second);

                if (cachingDuration > TimeSpan.Zero)
                {
                    _cacheProvider.AddOrUpdate(cacheKey, methodResult, cachingDuration);
                }
                else
                {
                    _cacheProvider.AddOrUpdate(_cacheKey, methodResult, new TimeSpan(0, 10, 0));
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException(ex.Message, ex);
                throw;
            }
        }