public void Intercept(IInvocation invocation) { var attributes1 = invocation.GetConcreteMethodInvocationTarget().GetCustomAttributes(typeof(BaseAttribute), true); var attributes2 = invocation.GetConcreteMethodInvocationTarget().DeclaringType.GetCustomAttributes(typeof(BaseAttribute), true); var attributes3 = invocation.GetConcreteMethod().GetCustomAttributes(typeof(BaseAttribute), true); var attributes4 = invocation.GetConcreteMethod().DeclaringType.GetCustomAttributes(typeof(BaseAttribute), true); var attributes = new object[attributes1.Length + attributes2.Length + attributes3.Length + attributes4.Length]; attributes1.CopyTo(attributes, 0); attributes2.CopyTo(attributes, attributes1.Length); attributes3.CopyTo(attributes, attributes1.Length + attributes2.Length); attributes4.CopyTo(attributes, attributes1.Length + attributes2.Length + attributes3.Length); var attrList = new ArrayList(); foreach (var attr in attributes) { if (!attrList.Contains(attr)) { attrList.Add(attr); } } if (attributes.Length == 0) { invocation.Proceed(); return; } ExecIntercept(invocation, attrList); }
public void Intercept(IInvocation invocation) { Debug.WriteLine(string.Empty); Debug.WriteLine("CollectionInterceptor"); Debug.WriteLine($"Invocation.Method: {invocation.Method.Name}"); Debug.WriteLine($"Invocation.InvocationTarget: {invocation.InvocationTarget.GetType().Name}"); Debug.WriteLine($"Invocation.TargetType: {invocation.TargetType.Name}"); Debug.WriteLine($"Invocation.GenericArguments: {string.Join(", ", invocation.GenericArguments?.Select(a => a.Name) ?? new[] { "" })}"); Debug.WriteLine($"Invocation.Proxy: {invocation.Proxy.GetType().Name}"); Debug.WriteLine($"Invocation.MethodInvocationTarget: {invocation.MethodInvocationTarget.Name}"); Debug.WriteLine(string.Empty); var generator = Program.ProxyGenerator; var methodName = invocation.Method.Name; if (invocation.Method.ReturnType.IsGenericType && invocation.Method.ReturnType.GetGenericTypeDefinition() == typeof(IEnumerator <>)) { var source = invocation.GetConcreteMethodInvocationTarget() .Invoke(invocation.InvocationTarget, invocation.Arguments); var proxy = generator.CreateInterfaceProxyWithTarget(typeof(IEnumerator <>).MakeGenericType(invocation.Method.ReturnType.GetGenericArguments().First()), Activator.CreateInstance(typeof(ProxyingEnumerator <>).MakeGenericType(invocation.Method.ReturnType.GetGenericArguments().First()), source), new IEnumeratorInterceptor()); invocation.ReturnValue = proxy; return; } invocation.Proceed(); }
public void Intercept(IInvocation invocation) { if (invocation.GetConcreteMethodInvocationTarget() .CustomAttributes .Any(x => x.AttributeType == typeof(AppMethodAuthorizeAttribute))) { var appMethodAuthorizeAttribute = invocation .GetConcreteMethodInvocationTarget() .GetCustomAttribute(typeof(AppMethodAuthorizeAttribute)) as AppMethodAuthorizeAttribute; AuthorizeInterceptorFactory.CreateAuthorizeInterceptor(appMethodAuthorizeAttribute) .Authorize(invocation); } invocation.Proceed(); }
static object InvokeOnCurrentInstance(IInvocation invocation) { Type pluginType = invocation.TargetType; EventDetails eventDetails = EnsureInitializedAndEnabled(pluginType); MethodInfo method = invocation.GetConcreteMethodInvocationTarget(); return method.Invoke(eventDetails.Event, invocation.Arguments); }
static object InvokeOnCurrentInstance(IInvocation invocation) { Type pluginType = invocation.TargetType; EventDetails eventDetails = EnsureInitializedAndEnabled(pluginType); MethodInfo method = invocation.GetConcreteMethodInvocationTarget(); return(method.Invoke(eventDetails.Event, invocation.Arguments)); }
private async Task Commit(IInvocation invocation) { var attr = invocation.GetConcreteMethodInvocationTarget().GetCustomAttribute <UnitOfWorkAttribute>(); if (attr == null || attr.Enable) { await _unitOfWork.Commit(); } }
protected override void MakeInterception(IInvocation invocation) { var parameters = invocation.GetConcreteMethodInvocationTarget().GetParameters().Select(a => a.Name); Logger.DebugFormat("{0} - TargetType: {1}", InterceptorName, invocation.TargetType); Logger.DebugFormat("{0} - Method.Name: {1}", InterceptorName, invocation.Method.Name); Logger.DebugFormat("{0} - Parameters: {1}", InterceptorName, string.Join(", ", parameters)); invocation.Proceed(); }
/// <summary> /// Intercepts the specified invocation. /// </summary> /// <param name="invocation">The invocation.</param> public void Intercept(IInvocation invocation) { MethodInfo method = invocation.GetConcreteMethodInvocationTarget(); RemoteMethodInfo remoteMethod = null; if (method.DeclaringType == typeof(IRemoteCaller)) { invocation.ReturnValue = method.Invoke(invocation.InvocationTarget, invocation.Arguments); return; } remoteMethod = InterfaceConfigLoader.GetServiceInfo(method); if (!remoteMethod.Offline) { if (invocation.Method.IsDefined(typeof(ClientCacheAttribute), true)) { // 获取缓存结果 if (ClientCacheManager.HasCache(method, invocation.Arguments)) { invocation.ReturnValue = ClientCacheManager.GetCachedData(method, invocation.Arguments); } else { // 第一次调用 object result = InvokeCommand(method, invocation.Arguments); ClientCacheManager.RegisterCache(method, result, remoteMethod.DataUpdateEvent, invocation.Arguments); invocation.ReturnValue = result; } } else { invocation.ReturnValue = InvokeCommand(method, invocation.Arguments); } } else { if (invocation.Method.IsDefined(typeof(ClientCacheAttribute), true)) { if (ClientCacheManager.HasCache(method, invocation.Arguments)) { invocation.ReturnValue = ClientCacheManager.GetCachedData(method, invocation.Arguments); } else { object result = InvokeCommand(method, invocation.Arguments); ClientCacheManager.RegisterCache(method, result, remoteMethod.DataUpdateEvent, invocation.Arguments); invocation.ReturnValue = result; } } else { OfflineProcess(invocation.Method, invocation.Arguments); // 调用离线处理方法 } } }
private void postTargetSub(StrandSchedulerDecorator strand, IInvocation invocation) { void postAction() { invocation.GetConcreteMethodInvocationTarget() .Invoke(invocation.InvocationTarget, invocation.Arguments); //Problems with Castle implementation (call from other thread does not work after upgrade to 4.3.1. version) //invocation.Proceed(); } strand.Post((Action)postAction); }
protected virtual Task InvokeTargetMethod(IInvocation invocation) { if (invocation.InvocationTarget.GetType() == invocation.Proxy.GetType()) { invocation.Proceed(); return(invocation.ReturnValue as Task); } MethodInfo targetMethod = invocation.GetConcreteMethodInvocationTarget(); return(targetMethod.Invoke(invocation.InvocationTarget, invocation.Arguments) as Task); }
protected virtual Task <T> InvokeTargetMethod <T>(IInvocation invocation) { // check if the target is Castle.Proxy, it should use invocation.Proceed() ! if (invocation.InvocationTarget.GetType() == invocation.Proxy.GetType()) { invocation.Proceed(); return(invocation.ReturnValue as Task <T>); } MethodInfo targetMethod = invocation.GetConcreteMethodInvocationTarget(); return(targetMethod.Invoke(invocation.InvocationTarget, invocation.Arguments) as Task <T>); }
public void Intercept(IInvocation invocation) { var method = invocation.GetConcreteMethodInvocationTarget(); if (null == method) { invocation.ReturnValue = invocation.Method.Invoke(get_the_implementation(), invocation.Arguments); } else { invocation.ReturnValue = method.Invoke(get_the_implementation(), invocation.Arguments); } }
public void Intercept(IInvocation invocation) { var joinPoint = new JoinPoint { Arguments = invocation.Arguments, MethodInfo = invocation.GetConcreteMethodInvocationTarget(), ReturnValue = invocation.ReturnValue, TargetObject = invocation.InvocationTarget, TargetType = invocation.TargetType, ExecuteMethodFromProxy = (() => { invocation.Proceed(); return invocation.ReturnValue; }), SetReturnValueToProxy = ((o) => invocation.ReturnValue = o) }; var typesToApply = _types.Where(x => _pointCut.CanApply(joinPoint, x)).ToArray(); if (typesToApply.Length > 0) { var aspectsToApply = typesToApply.Select(x => _container.Resolve(x) as IAspect).ToArray(); aspectsToApply = aspectsToApply.OrderBy(x => x.GetOrder(joinPoint)).ToArray(); var root = aspectsToApply[0]; var aspect = root; for (var i = 1; i < aspectsToApply.Length; i++) { aspect.NextAspect = aspectsToApply[i]; aspect = aspect.NextAspect; } root.Apply(joinPoint); } else { invocation.Proceed(); } }
/// <summary> /// Intercepts a method. /// </summary> /// <param name="invocation">Method invocation arguments</param> public void Intercept(IInvocation invocation) { var unitOfWorkAttr = UnitOfWorkAttribute.GetUnitOfWorkAttributeOrNull(invocation.GetConcreteMethodInvocationTarget()); if (unitOfWorkAttr == null || unitOfWorkAttr.IsDisabled) { //No need to a uow invocation.Proceed(); return; } //No current uow, run a new one PerformUow(invocation, unitOfWorkAttr.CreateOptions()); }
private static string GetCacheKey(IInvocation invocation) { var fullName = invocation.TargetType.FullName; var method = invocation.Method.Name; var argumentNames = invocation.GetConcreteMethodInvocationTarget().GetParameters().Select(a => a.Name); var argumentValues = string.Join("_", invocation.Arguments); return string.Format( "{0}_{1}_{2}_{3}", fullName, method, string.Join("_", argumentNames), argumentValues); }
private void postTargetFunc(StrandSchedulerDecorator strand, IInvocation invocation) { var methodInvocationTarget = invocation.MethodInvocationTarget; dynamic proxyTcs = null; proxyTcs = getProxyTcs(methodInvocationTarget); var isGenericReturnType = methodInvocationTarget.ReturnType.IsGenericType; invocation.ReturnValue = proxyTcs.Task; strand.Post(postFunc); Task postFunc() { Task resultTask = null; var hasException = false; try { resultTask = invocation.GetConcreteMethodInvocationTarget() .Invoke(invocation.InvocationTarget, invocation.Arguments) as Task; //Problems with Castle implementation (call from other thread does not work after upgrade to 4.3.1. version) //invocation.Proceed(); } catch (Exception e) { hasException = true; if (resultTask == null) { resultTask = TaskEx.TaskFromException(e); } } finally { if (!hasException && isGenericReturnType) { TaskEx.PrepareTcsTaskFromExistingTask((dynamic)resultTask, proxyTcs); } else { TaskEx.PrepareTcsTaskFromExistingTask(resultTask, proxyTcs); } } return(resultTask); } }
/// <summary>Intercepts the specified invocation.</summary> /// <param name="invocation">The invocation.</param> /// <autogeneratedoc /> /// TODO Edit XML Comment Template for Intercept public void Intercept(IInvocation invocation) { var customAttributes = Attribute.GetCustomAttributes( invocation .GetConcreteMethodInvocationTarget( ) , typeof(PushContextAttribute) ); if (invocation.InvocationTarget is IHaveLogger haveLogger) { var logger = haveLogger.Logger; logger?.Trace($"invocation of {invocation.Method.Name}"); } invocation.Proceed( ); }
/// <summary> /// Intercepts the specified invocation. /// </summary> /// <param name="invocation">The invocation.</param> public void Intercept(IInvocation invocation) { MethodInfo method = invocation.GetConcreteMethodInvocationTarget(); RemoteMethodInfo remoteMethod = null; if (method.DeclaringType == typeof(IRemoteCaller)) { invocation.ReturnValue = method.Invoke(invocation.InvocationTarget, invocation.Arguments); return; } remoteMethod = InterfaceConfigLoader.GetServiceInfo(method); if (!remoteMethod.Offline) { if (invocation.Method.IsDefined(typeof(ClientCacheAttribute), true)) { // 获取缓存结果 if (ClientCacheManager.HasCache(method, invocation.Arguments)) invocation.ReturnValue = ClientCacheManager.GetCachedData(method, invocation.Arguments); else { // 第一次调用 object result = InvokeCommand(method, invocation.Arguments); ClientCacheManager.RegisterCache(method, result, remoteMethod.DataUpdateEvent, invocation.Arguments); invocation.ReturnValue = result; } } else invocation.ReturnValue = InvokeCommand(method, invocation.Arguments); } else { if (invocation.Method.IsDefined(typeof(ClientCacheAttribute), true)) { if (ClientCacheManager.HasCache(method, invocation.Arguments)) invocation.ReturnValue = ClientCacheManager.GetCachedData(method, invocation.Arguments); else { object result = InvokeCommand(method, invocation.Arguments); ClientCacheManager.RegisterCache(method, result, remoteMethod.DataUpdateEvent, invocation.Arguments); invocation.ReturnValue = result; } } else OfflineProcess(invocation.Method, invocation.Arguments); // 调用离线处理方法 } }
/// <summary> /// 拦截方法 打印被拦截的方法执行前的名称、参数和方法执行后的 返回结果 /// </summary> /// <param name="invocation">包含被拦截方法的信息</param> public void Intercept(IInvocation invocation) { Debug.WriteLine("你正在调用方法 \"{0}\" 参数是 {1}... " + invocation.Method.Name + string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())); //获取arrts属性 var attrs = invocation.GetConcreteMethodInvocationTarget().GetCustomAttributes(typeof(BaseAspectAttribute), true); if (attrs.Length == 0) { invocation.Proceed(); return; } //指定Attribute执行方法 AttributeRecursionIntercept(invocation, attrs); //在被拦截的方法执行完毕后 继续执行 Debug.WriteLine("方法执行完毕,返回结果:{0}", invocation.ReturnValue); }
public void Intercept(IInvocation invocation) { if (this.hasCacheAttribute(invocation.GetConcreteMethodInvocationTarget())) { string keyName = invocation.Method.Name + string.Join(",", invocation.Arguments.Select(x => x.ToString())); var cached = MemoryCache.Default.Get(keyName); if (cached == null) { invocation.Proceed(); var result = invocation.ReturnValue; MemoryCache.Default.Add(keyName, result, new CacheItemPolicy()); } else { invocation.ReturnValue = cached; } } else { invocation.Proceed(); } }
public void Intercept(IInvocation invocation) { var attributes = _attributeEngine.AutoFindAttribute(""); foreach (var attribute in attributes) { var nameTemp = GetAttributeName(GetScanTaskName(attribute.FullName)); var type = Type.GetType(nameTemp); if (type != null) { var existedMethod = Attribute.GetCustomAttribute(invocation.GetConcreteMethodInvocationTarget(), type, false); if (existedMethod != null) { var localAttribute = attribute; CatchExceptionHelper.TryCatchAction(() => _attributeEngine.Run(invocation, localAttribute), _logger); } } } }
/// <summary> /// Intercepts and performs timing logic /// </summary> /// <param name="invocation">current invocation site</param> protected override sealed void OnIntercept(IInvocation invocation) { invocation = Arguments.EnsureNotNull(invocation, nameof(invocation)); var start = this.timer.ElapsedTicks; try { invocation.Proceed(); } finally { var end = this.timer.ElapsedTicks; var duration = start - end; var concreteClass = invocation.GetConcreteMethodInvocationTarget(); var concreteMethod = invocation.GetConcreteMethod(); this.Logger.Debug("Class:{0} Method:{1} Duration:{2}", concreteClass.Name, concreteMethod.Name, duration); } }
protected override void MakeInterception(IInvocation invocation) { var args = invocation.GetConcreteMethodInvocationTarget().GetParameters(); var failedArgs = args .Where(a => a.IsDefined(typeof (NotNullAttribute), true) && (invocation.Arguments[a.Position] == null)); if (failedArgs.Any()) { Logger.DebugFormat( "{0} - Argument(s) that failed NotNull-inspection: {1}", InterceptorName, string.Join(", ", failedArgs.Select(a => a.Name))); throw new ArgumentNullException(failedArgs.First().Name); } Logger.DebugFormat("{0} - Arguments passed NotNull-inspection.", InterceptorName); invocation.Proceed(); }
public MethodInfo GetConcreteMethodInvocationTarget() { return(_rootInvocation.GetConcreteMethodInvocationTarget()); }
public MethodInfo GetConcreteMethodInvocationTarget() { return(parent.GetConcreteMethodInvocationTarget()); }
private TransactionsContainer StartTransactions([NotNull] IInvocation invocation) { MethodInfo method = invocation.GetConcreteMethodInvocationTarget(); return(this.transactionCoordinator.StartTransactionsFor(method)); }
public void Intercept(IInvocation invocation) { Console.WriteLine("执行前..."); // 判断当前方法是否包含 IgnoreCache 特性 var method = invocation.GetConcreteMethod(); // 调用的具体方法,接口、具体类 var targetMethod = invocation.GetConcreteMethodInvocationTarget(); // 获取具体的调用目标方法,也就是实际执行的方法 // 忽略缓存 var ignoreAttr = this.GetAttribute <IgnoreCacheAttribute>(method, targetMethod); if (ignoreAttr != null) { Console.WriteLine("忽略缓存..."); // 执行原本方法 invocation.Proceed(); return; } Console.WriteLine("读取本地缓存..."); // 本地缓存配置 var memoryOptions = this.GetAttribute <MemoryCacheOptionsAttribute>(method, targetMethod); // 生成缓存 key var memoryCacheKey = this.GenericCacheKey(invocation, memoryOptions.KeyPrefix); // Redis缓存获取 var redisOptions = this.GetAttribute <RedisCacheOptionsAttribute>(method, targetMethod); // 读取本地缓存 var cacheValue = _memoryCache.Get(memoryCacheKey); if (cacheValue == null) { Console.WriteLine("读取Redis缓存..."); // 生成缓存 key var redisCacheKey = this.GenericCacheKey(invocation, redisOptions.KeyPrefix); cacheValue = _distributedCache.Get(redisCacheKey); } if (cacheValue != null) { Console.WriteLine("读取缓存..."); //将当前获取到的缓存值,赋值给当前执行方法 invocation.ReturnValue = cacheValue; return; } // 执行原本方法 invocation.Proceed(); //存入缓存 if (!string.IsNullOrWhiteSpace(memoryCacheKey)) { Console.WriteLine("写入本地缓存..."); // 写入本地缓存 var entryOptions = new MemoryCacheEntryOptions() .SetSize(1) // (TimeSpan.FromSeconds((double)memoryOptions.AbsoluteExpiration)) .SetSlidingExpiration(TimeSpan.FromSeconds((double)memoryOptions.SlidingExpiration)); if (memoryOptions.AbsoluteExpiration > 0) { entryOptions.SetAbsoluteExpiration(DateTime.Now.AddSeconds((double)memoryOptions.AbsoluteExpiration)); } _memoryCache.Set(memoryCacheKey, invocation.ReturnValue, entryOptions); Console.WriteLine("写入Redis缓存..."); // 写入Redis缓存 var redisEntryOptions = new DistributedCacheEntryOptions() { SlidingExpiration = TimeSpan.FromSeconds((double)redisOptions.SlidingExpiration) }; if (redisOptions.AbsoluteExpiration > 0) { redisEntryOptions.SetAbsoluteExpiration(DateTime.Now.AddSeconds((double)redisOptions.AbsoluteExpiration)); } _distributedCache.SetString(memoryCacheKey, System.Text.Json.JsonSerializer.Serialize(invocation.ReturnValue), redisEntryOptions); } Console.WriteLine("执行后..."); }
public object CreateErrorResponse(IInvocation invocation) { return CreateFailedServiceReply(invocation.GetConcreteMethodInvocationTarget().ReturnType, new ServiceFault(messageProvider.GetFault(SystemMessages.GeneralErrorDuringRequest))); }
public object CreateErrorResponse(IInvocation invocation, BusinessException exception) { return CreateFailedServiceReply(invocation.GetConcreteMethodInvocationTarget().ReturnType, new ServiceFault(exception)); }
public bool Handle(IInvocation invocation) { return VerifyReturnTypeIsServiceReply(invocation.GetConcreteMethodInvocationTarget().ReturnType); }
protected override MethodInfo GetMethodInfoFromInterceptor() { var method = _invocation.GetConcreteMethodInvocationTarget(); return(method); }