public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { IMethodReturn returnValue = null; //顺序执行 returnValue = getNext()(input, getNext); //若符合要求 if (input.MethodBase is MethodInfo && (input.MethodBase as MethodInfo).ReturnType == typeof(bool)) { //根据异常状态设置返回值 if (null != returnValue.Exception) { returnValue.ReturnValue = true; } else { returnValue.ReturnValue = false; } //设置返回值类型 returnValue = CustomMethodReturn.PrepareCustomReturn(input, returnValue); } return(returnValue); }
/// <summary> /// 自定义返回值当异常时 /// </summary> /// <param name="input"></param> /// <param name="returnValue"></param> /// <returns></returns> internal static IMethodReturn PrepareCustomReturn(IMethodInvocation input, IMethodReturn returnValue) { //若有异常 if (returnValue.Exception != null) { //清空异常 returnValue.Exception = null; var tempReturn = new CustomMethodReturn(); tempReturn.Exception = returnValue.Exception; tempReturn.ReturnValue = returnValue.ReturnValue; tempReturn.InvocationContext = returnValue.InvocationContext; List <ParameterInfo> lstParameterInfo = new List <ParameterInfo>(); List <object> lstOutPutValue = new List <object>(); PrepareOutPutParameter(input, lstParameterInfo, lstOutPutValue); //设置输出值 tempReturn.Outputs = new ParameterCollection(lstOutPutValue.ToArray(), lstParameterInfo.ToArray(), k => true); //若输出参数个数>0 if (lstParameterInfo.Count > 0) { returnValue = tempReturn; } } return(returnValue); }
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { IMethodReturn returnValue = null; //若符合要求 if (input.MethodBase is MethodInfo && (input.MethodBase as MethodInfo).ReturnType == typeof(bool)) { //开启连接上下文 using (var ctx = m_useContextCreater.CreatDbContext()) { //开启事务 using (var ta = ctx.Database.BeginTransaction()) { returnValue = getNext()(input, getNext); //若没有异常则提交事务 if (null == returnValue.Exception) { //事务提交 ta.Commit(); //设置返回值 returnValue.ReturnValue = true; } else { //事务回滚 ta.Rollback(); //设置返回值 returnValue.ReturnValue = false; } //设置返回值类型 returnValue = CustomMethodReturn.PrepareCustomReturn(input, returnValue); } } } //正常执行 else { returnValue = getNext()(input, getNext); } return(returnValue); }
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { IMethodReturn returnValue = null; //若符合要求 if (input.MethodBase is MethodInfo && (input.MethodBase as MethodInfo).ReturnType == typeof(bool)) { //开启事务 using (TransactionScope useScope = new TransactionScope(TransactionScopeOption.RequiresNew, m_useTrancsactionOptions)) { returnValue = getNext()(input, getNext); //若没有异常则提交事务 if (null == returnValue.Exception) { useScope.Complete(); //设置返回值 returnValue.ReturnValue = true; } else { //设置返回值 returnValue.ReturnValue = false; } //设置返回值类型 returnValue = CustomMethodReturn.PrepareCustomReturn(input, returnValue); } } //正常执行 else { returnValue = getNext()(input, getNext); } return(returnValue); }