public override IMessage Invoke(IMessage msg) { var methodCall = msg as IMethodCallMessage; var methodInfo = methodCall.MethodBase as MethodInfo; var instanceMethod = this.GetMethod(this._decorated, methodInfo); try { var delegateMethod = DynamicMethod.BuildMethod(instanceMethod); var authenticationAttribute = instanceMethod.GetCustomAttribute <AuthenticationAttribute>(); if (authenticationAttribute != null) { authenticationAttribute.Validate(); } var result = delegateMethod.Invoke(this._decorated, methodCall.InArgs); return(new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall)); } catch (Exception e) { var exceptionAttribute = instanceMethod.GetCustomAttribute <ExceptionHandlerAttribute>(); if (exceptionAttribute != null) { exceptionAttribute.CatchException(e); } return(new ReturnMessage(e, methodCall)); } }
public override IMessage Invoke(IMessage msg) { var methodCall = msg as IMethodCallMessage; var methodInfo = methodCall.MethodBase as MethodInfo; var method = DynamicMethod.BuildMethod(methodInfo); var principal = Thread.CurrentPrincipal; if (!principal.Identity.IsAuthenticated) { throw new Exception("沒有通過驗證"); } if (!principal.IsInRole("Admin")) { throw new Exception("沒有在Admin群裡"); } try { var result = method.Invoke(this._decorated, methodCall.InArgs); return(new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall)); } catch (Exception e) { return(new ReturnMessage(e, methodCall)); } }
public void Intercept(IInvocation invocation) { var instanceMethod = invocation.MethodInvocationTarget; var instanceArgs = invocation.Arguments; var instance = invocation.InvocationTarget; try { var delegateMethod = DynamicMethod.BuildMethod(instanceMethod); var authenticationAttribute = instanceMethod.GetCustomAttribute <AuthenticationAttribute>(); if (authenticationAttribute != null) { authenticationAttribute.Validate(); } delegateMethod.Invoke(instance, instanceArgs); } catch (Exception e) { var exceptionAttribute = instanceMethod.GetCustomAttribute <ExceptionHandlerAttribute>(); if (exceptionAttribute != null) { exceptionAttribute.CatchException(e); } throw; } }
public override IMessage Invoke(IMessage msg) { var methodCall = msg as IMethodCallMessage; var methodInfo = methodCall.MethodBase as MethodInfo; var method = DynamicMethod.BuildMethod(methodInfo); try { var result = method.Invoke(this._decorated, methodCall.InArgs); return(new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall)); } catch (Exception e) { return(new ReturnMessage(e, methodCall)); } }