private void PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options) { var uow = _unitOfWorkManager.Begin(options); try { invocation.Proceed(); } catch { uow.Dispose(); throw; } if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)invocation.ReturnValue, async() => await uow.CompleteAsync(), exception => uow.Dispose() ); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, async() => await uow.CompleteAsync(), exception => uow.Dispose() ); } }
private void PerformAsyncUow(IInvocation invocation) { Console.WriteLine("异步方法事务开启"); var uow = _admUnitOfWork.Begin(); try { invocation.Proceed(); } catch { uow.Dispose(); throw; } if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)invocation.ReturnValue, async() => await _admUnitOfWork.CompleteAsync(uow), exception => uow.Dispose() ); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, async() => await _admUnitOfWork.CompleteAsync(uow), exception => uow.Dispose() ); } }
public static T PerformAsyncUow <T>(this IUnitOfWorkManager unitOfWorkManager, Func <T> action, UnitOfWorkOptions options = null) { var uow = unitOfWorkManager.Begin(options ?? DefaultUoWOptions); object returnValue; try { returnValue = action(); } catch { uow.Dispose(); throw; } if (typeof(T) == typeof(Task)) { returnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)returnValue, async() => await uow.CompleteAsync(), exception => uow.Dispose()); } else //Task<TResult> { returnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( typeof(T).GenericTypeArguments[0], returnValue, async() => await uow.CompleteAsync(), exception => uow.Dispose()); } return((T)returnValue); }
/// <summary> /// 实例化IInterceptor唯一方法 /// </summary> /// <param name="invocation">包含拦截方法信息</param> public void Intercept(IInvocation invocation) { //记录被拦截方法信息的日志信息 var dataIntercept = "" + $"【当前执行方法】:{ invocation.Method.Name} \r\n" + $"【携带的参数有】: {string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())} \r\n"; try { MiniProfiler.Current.Step($"执行Service方法:{invocation.Method.Name}() -> "); //在被拦截的方法执行完毕后 继续执行当前方法,注意是被拦截的是异步的 invocation.Proceed(); // 异步获取异常,先执行 if (IsAsyncMethod(invocation.Method)) { //Wait task execution and modify return value if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)invocation.ReturnValue, async() => await TestActionAsync(invocation), ex => { LogEx(ex, ref dataIntercept); }); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, async() => await TestActionAsync(invocation), ex => { LogEx(ex, ref dataIntercept); }); } } else {// 同步1 } } catch (Exception ex)// 同步2 { LogEx(ex, ref dataIntercept); } dataIntercept += ($"【执行完成结果】:{invocation.ReturnValue}"); Parallel.For(0, 1, e => { LogLock.OutSql2Log("AOPLog", new string[] { dataIntercept }); }); _hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData()).Wait(); }
public override void Intercept(IInvocation invocation) { MethodInfo methodInfo; try { methodInfo = invocation.MethodInvocationTarget; } catch { methodInfo = invocation.GetConcreteMethod(); } if (!UnitOfWorkHelper.IsUnitOfWorkMethod(invocation.Method, out var unitOfWorkAttribute)) { invocation.Proceed(); } else { using (var unitOfWork = _unitOfWorkManager.Begin(CreateOptions(invocation, unitOfWorkAttribute))) { invocation.Proceed(); if (!invocation.Method.IsAsync()) { unitOfWork.Complete(); } else { if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)invocation.ReturnValue, async() => { await unitOfWork.CompleteAsync(); await Task.FromResult(0); }, exception => unitOfWork.Dispose() ); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, async() => { await unitOfWork.CompleteAsync(); await Task.FromResult(0); }, exception => unitOfWork.Dispose() ); } } } } }
/// <summary> /// 实例化IInterceptor唯一方法 /// </summary> /// <param name="invocation">包含被拦截方法的信息</param> public void Intercept(IInvocation invocation) { var method = invocation.MethodInvocationTarget ?? invocation.Method; //对当前方法的特性验证 //如果需要验证 if (method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(UseTranAttribute)) is UseTranAttribute) { try { Console.WriteLine($"Begin Transaction"); _unitOfWork.BeginTran(); invocation.Proceed(); // 异步获取异常,先执行 if (IsAsyncMethod(invocation.Method)) { if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)invocation.ReturnValue, async() => await TestActionAsync(invocation), ex => { _unitOfWork.RollbackTran(); }); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, async() => await TestActionAsync(invocation), ex => { _unitOfWork.RollbackTran(); }); } } _unitOfWork.CommitTran(); } catch (Exception) { Console.WriteLine($"Rollback Transaction"); _unitOfWork.RollbackTran(); } } 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 ProceedAndCallPostTreatment <T>(IInvocation invocation, List <T> pEntities, bool IsAsync) { invocation.Proceed(); if (IsAsync) { ////Wait task execution and modify return value if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)invocation.ReturnValue, async() => { GenericPostTreatment(invocation, pEntities); }, ex => { }); } else //Task<TResult> { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, async(e) => { invocation.ReturnValue = e; GenericPostTreatment(invocation, pEntities); }, ex => { }); } } else { this.GenericPostTreatment(invocation, pEntities); } }
public void Intercept(IInvocation invocation) { // 记录被拦截方法信息的日志信息 var interceptorInfo = "" + $"【当前执行方法】:{ invocation.Method.Name} \r\n" + $"【携带的参数有】: {string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())} \r\n"; try { // 在被拦截的方法执行完毕后 继续执行当前方法,注意是被拦截的是异步的 invocation.Proceed(); // 异步获取异常,先执行 if (IsAsyncMethod(invocation.Method)) { #region 方案一 // Wait task execution and modify return value if (invocation.Method.ReturnType == typeof(Task)) { invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( (Task)invocation.ReturnValue, async() => await SuccessAction(invocation, interceptorInfo), /*成功时执行*/ ex => { System.Console.WriteLine(ex.Message); }); } // Task<TResult> else { invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( invocation.Method.ReturnType.GenericTypeArguments[0], invocation.ReturnValue, async(o) => await SuccessAction(invocation, interceptorInfo, o),/*成功时执行*/ ex => { System.Console.WriteLine(ex); }); } #endregion // 如果方案一不行,试试这个方案 #region 方案二 //var type = invocation.Method.ReturnType; //var resultProperty = type.GetProperty("Result"); //dataIntercept += ($"【执行完成结果】:{JsonConvert.SerializeObject(resultProperty.GetValue(invocation.ReturnValue))}"); //Parallel.For(0, 1, e => //{ // LogLock.OutSql2Log("AOPLog", new string[] { dataIntercept }); //}); #endregion } else { // 同步1 interceptorInfo += ($"【执行完成结果】:{invocation.ReturnValue}"); Parallel.For(0, 1, e => { System.Console.WriteLine(interceptorInfo); }); } } catch (Exception ex)// 同步2 { System.Console.WriteLine(ex.Message + interceptorInfo); } }