private void PerformAsync(IInvocation invocation) { var fullName = invocation.Method.DeclaringType.FullName + "." + invocation.Method.Name; PerformSync(invocation); if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithFinally( (Task)invocation.ReturnValue, ex => { ExceptionHanding(ex, fullName, invocation.Arguments); }); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, ex => { ExceptionHanding(ex, fullName, invocation.Arguments); }); } }
public async Task Should_Call_Finally_On_Success() { var calledFinally = false; await InternalAsyncHelper.AwaitTaskWithFinally( MyMethod1Async(), exception => { calledFinally = true; exception.ShouldBe(null); }); calledFinally.ShouldBe(true); }
public async Task Should_Call_Finally_On_Exception() { var calledFinally = false; await Assert.ThrowsAsync <Exception>(async() => { await InternalAsyncHelper.AwaitTaskWithFinally( MyMethod1Async(true), exception => { calledFinally = true; exception.ShouldNotBe(null); exception.Message.ShouldBe("test exception"); }); }); calledFinally.ShouldBe(true); }
private void PerformAsyncAuditing(IInvocation invocation, AuditInfo auditInfo) { var stopwatch = Stopwatch.StartNew(); invocation.Proceed(); if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithFinally((Task)invocation.ReturnValue, exception => SaveAuditInfo(auditInfo, stopwatch, exception)); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, exception => SaveAuditInfo(auditInfo, stopwatch, exception) ); } }
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; } } }