public void Intercept(IInvocation invocation) { if (ShouldIntercept(invocation)) { Intercept(invocation, InternalAsyncHelper.IsAsyncMethod(invocation.Method)); } else { invocation.Proceed(); } }
public void Intercept(IInvocation invocation) { var method = invocation.MethodInvocationTarget ?? invocation.Method; var transactionAtt = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(UnitOfWorkAttribute)); if (transactionAtt is UnitOfWorkAttribute) { try { Console.WriteLine($"Begin Transaction"); _unitOfWork.BeginTran(); invocation.Proceed(); // 异步获取异常,先执行 if (InternalAsyncHelper.IsAsyncMethod(invocation.Method)) { if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)invocation.ReturnValue, ex => { _unitOfWork.RollbackTran(); }); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, ex => { _unitOfWork.RollbackTran(); }); } } _unitOfWork.CommitTran(); } catch (Exception ex) { Console.WriteLine(ex); _unitOfWork.RollbackTran(); } } else { //如果没有标记[UnitOfWork],直接执行方法 invocation.Proceed(); } }
private void ValidateBeforeProceeding(IInvocation invocation) { //le invocation vas être destiné (target) a un IBaseValidatedAppService, car dans le ValidationRegistrar dans la méthode Kernel_ComponentRegistered //seulement les IBaseRestApplicationService sont handle par l'Interceptor. IBaseValidatedAppService appService = invocation.InvocationTarget as IBaseValidatedAppService; string assemblyName = invocation.InvocationTarget.GetType().BaseType.Assembly.ManifestModule.Name.Replace(".dll", "."); string validatorName = "I" + appService.GetType().BaseType.Name + "Validator"; TypeResolver typeResolver = _iocResolver.Resolve <TypeResolver>(); Type validatorInterfaceType = typeResolver[assemblyName + validatorName]; if (validatorInterfaceType is null) { return; } IBaseValidator baseValidator = _iocResolver.Resolve(validatorInterfaceType) as IBaseValidator; Type validatorType = baseValidator.GetType(); //IocManager.Instance.IocContainer.Resolve(""); //on vas essayer d'aller chercher par réflection les méthode de validation //on vas devoir avoir un standard que les méthode dans les Validator qui hérite de IBaseValidation //doivent avoir le même nom que la méthode du app service qu'elle valide plus le terme Validation string methodName = invocation.MethodInvocationTarget.Name + "Validation"; MethodInfo method = validatorType.GetMethod(methodName); if (method != null) { //on invoke la méthode du validator //on doit faire le try catch et le re-throw ici sinon on perdait le type de l'exception try { if (InternalAsyncHelper.IsAsyncMethod(method)) { var returnValue = method.Invoke(baseValidator, invocation.Arguments); ////Wait task execution and modify return value if (method.ReturnType == typeof(Task)) { returnValue = InternalAsyncHelper.AwaitTaskWithFinally( (Task)returnValue, ex => { invocation.Proceed(); }); } else //Task<TResult> { returnValue = InternalAsyncHelper.CallAwaitTaskWithFinallyAndGetResult( method.ReturnType.GenericTypeArguments[0], returnValue, ex => { invocation.Proceed(); }); } } else { invocation.Proceed(); } } catch (Exception ex) { throw ex.InnerException; } } else { } }
private void PreTreatmentBeforeProceeding(IInvocation invocation) { IPreTreatmentAppService appService = invocation.InvocationTarget as IPreTreatmentAppService; string assemblyName = invocation.InvocationTarget.GetType().BaseType.Assembly.ManifestModule.Name.Replace(".dll", "."); string preTreatmentExecutorName = "I" + appService.GetType().BaseType.Name + "PreTreatmentExecutor"; TypeResolver typeResolver = _iocResolver.Resolve <TypeResolver>(); Type preTreatmentExecutorInterfaceType = typeResolver[assemblyName + preTreatmentExecutorName]; if (preTreatmentExecutorInterfaceType is null) { return; } IPreTreatmentExecutor preTreatmentExecutor = _iocResolver.Resolve(preTreatmentExecutorInterfaceType) as IPreTreatmentExecutor; Type preTreatmentExecutorType = preTreatmentExecutor.GetType(); string methodName = "PreTreatment_" + invocation.MethodInvocationTarget.Name; MethodInfo method = preTreatmentExecutorType.GetMethod(methodName); var request = invocation.Arguments[0]; if (method != null) { try { var returnValue = method.Invoke(preTreatmentExecutor, invocation.Arguments); if (InternalAsyncHelper.IsAsyncMethod(method)) { //Wait task execution and modify return value if (method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithFinally( (Task)returnValue, ex => { invocation.Proceed(); }); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithFinallyAndGetResult( method.ReturnType.GenericTypeArguments[0], returnValue, ex => { invocation.Proceed(); }); } } else { invocation.Proceed(); } } catch (Exception ex) { throw ex.InnerException; } } }