Ejemplo n.º 1
0
        /// <inheritdoc />
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            this.unitOfWorkAttribute = invocationContext.GetOwningAttribute() as UnitOfWorkAttribute;
            var dbContextType       = this.unitOfWorkAttribute?.DbContextType;
            var uowPropertyPosition = invocationContext.GetParameterPosition <IUnitOfWork>();
            var uow = invocationContext.GetParameterValue <IUnitOfWork>(uowPropertyPosition);

            // If the Unit of Work is already set - skip the interception
            if (uow != null)
            {
                return;
            }

            // Get the Generic Type Definition
            var methodInfo = this.unitOfWorkFactory.GetType().GetMethods().FirstOrDefault(p => p.IsGenericMethod && p.Name == nameof(this.unitOfWorkFactory.Create));

            // Build a method with the DB Context Type
            var method = methodInfo?.MakeGenericMethod(dbContextType);

            // Create the new UnitOfWork
            this.unitOfWork = (IUnitOfWork)method?.Invoke(this.unitOfWorkFactory, null);

            // Replace the UOW on the method with the new one
            invocationContext.SetParameterValue(uowPropertyPosition, this.unitOfWork);
        }
Ejemplo n.º 2
0
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            try
            {
                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);
                }

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


                var doesCacheExist = _cacheProvider.Get(_cacheKey);

                if (doesCacheExist != null)
                {
                    invocationContext.OverrideMethodReturnValue(doesCacheExist);
                    invocationContext.BypassInvocation();
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException(ex.Message, ex);
                throw;
            }
        }
        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);
                }

            }
        }
Ejemplo n.º 4
0
        protected override void BeforeInvoke(InvocationContext invocationContext)
        {
            _attribute = invocationContext.GetOwningAttribute() as UnitOfWorkAttribute;

            var uowParamPosition = invocationContext.GetParameterPosition <IUnitOfWork>();
            var uow = invocationContext.GetParameterValue <IUnitOfWork>(uowParamPosition);

            if (uow != null)
            {
                return;
            }

            invocationContext.SetParameterValue(uowParamPosition, _unitOfWork.Value);
        }
Ejemplo n.º 5
0
        private List <LogArgument> GetArguments(InvocationContext invocationContext)
        {
            var arguments = new List <LogArgument>();

            int _argumentsLength = invocationContext.GetInvocation().Arguments.Length;

            if (_argumentsLength > 0)
            {
                for (int i = 0; i < _argumentsLength; i++)
                {
                    arguments.Add(new LogArgument
                    {
                        Name = invocationContext.GetParameterValue(i).ToString()
                    });
                }
            }

            return(arguments);
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
0
        public void AfterInvoke(InvocationContext invocationContext, object methodResult)
        {
            try
            {
                var endDate       = DateTime.Now;
                var executionTime = (int)((endDate - _startTime).TotalMilliseconds);

                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 methodInfo = invocationContext.GetExecutingMethodInfo();

                var auditEntity = new Audit
                {
                    Id            = Guid.NewGuid(),
                    Input         = JsonConvert.SerializeObject(argsDictionary),
                    Output        = JsonConvert.SerializeObject(methodResult),
                    ClassName     = methodInfo.Module.ToString(),
                    MethodName    = methodInfo.Name,
                    StartTime     = _startTime,
                    EndTime       = endDate,
                    ExecutionTime = executionTime
                };

                _auditorProvider.Save(auditEntity);
            }
            catch (Exception ex)
            {
                _logger.ErrorException(ex.Message, ex);
                throw;
            }
        }
Ejemplo n.º 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;
            }
        }