/// <summary> /// Invokes a <paramref name="func"/> with a <typeparamref name="TResult"/> asynchronously. /// </summary> /// <typeparam name="TResult">The result <see cref="Type"/>.</typeparam> /// <param name="caller">The calling (invoking) object.</param> /// <param name="func">The function to invoke.</param> /// <param name="param">The optional parameter passed to the invoke.</param> /// <param name="memberName">The method or property name of the caller to the method.</param> /// <param name="filePath">The full path of the source file that contains the caller.</param> /// <param name="lineNumber">The line number in the source file at which the method is called.</param> /// <returns>The result.</returns> protected async override Task <TResult> WrapInvokeAsync <TResult>(object caller, Func <Task <TResult> > func, BusinessInvokerArgs param = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0) { Check.NotNull(func, nameof(func)); BusinessInvokerArgs bia = Check.NotNull(param ?? BusinessInvokerArgs.Default, nameof(param)); TransactionScope txn = null; DataContextScope ctx = null; OperationType ot = ExecutionContext.Current.OperationType; try { if (bia.IncludeTransactionScope) { txn = new TransactionScope(bia.TransactionScopeOption); } ctx = DataContextScope.Begin(bia.DataContextScopeOption); var result = await func().ConfigureAwait(false); if (txn != null) { txn.Complete(); } return(result); } catch (Exception ex) { bia.ExceptionHandler?.Invoke(ex); throw; } finally { if (ctx != null) { ctx.Dispose(); } if (txn != null) { txn.Dispose(); } ExecutionContext.Current.OperationType = ot; } }
/// <summary> /// Invokes a <paramref name="func"/> with a <typeparamref name="TResult"/> synchronously. /// </summary> /// <typeparam name="TResult">The result <see cref="Type"/>.</typeparam> /// <param name="caller">The calling (invoking) object.</param> /// <param name="func">The function to invoke.</param> /// <param name="param">The optional parameter passed to the invoke.</param> /// <param name="memberName">The method or property name of the caller to the method.</param> /// <param name="filePath">The full path of the source file that contains the caller.</param> /// <param name="lineNumber">The line number in the source file at which the method is called.</param> /// <returns>The result.</returns> protected override TResult WrapInvoke <TResult>(object caller, Func <TResult> func, BusinessInvokerArgs?param = null, [CallerMemberName] string?memberName = null, [CallerFilePath] string?filePath = null, [CallerLineNumber] int lineNumber = 0) { Check.NotNull(func, nameof(func)); BusinessInvokerArgs bia = param ?? BusinessInvokerArgs.Default; TransactionScope? txn = null; DataContextScope? ctx = null; OperationType ot = ExecutionContext.Current.OperationType; try { if (bia.IncludeTransactionScope) { txn = new TransactionScope(bia.TransactionScopeOption, TransactionScopeAsyncFlowOption.Enabled); } ctx = DataContextScope.Begin(bia.DataContextScopeOption); var result = func(); if (txn != null) { txn.Complete(); } return(result); } catch (Exception ex) { bia.ExceptionHandler?.Invoke(ex); throw; } finally { if (ctx != null) { ctx.Dispose(); } if (txn != null) { txn.Dispose(); } ExecutionContext.Current.OperationType = ot; } }
/// <summary> /// Invokes an <paramref name="action"/> synchronously. /// </summary> /// <param name="caller">The calling (invoking) object.</param> /// <param name="action">The function to invoke.</param> /// <param name="param">The optional parameter passed to the invoke.</param> /// <param name="memberName">The method or property name of the caller to the method.</param> /// <param name="filePath">The full path of the source file that contains the caller.</param> /// <param name="lineNumber">The line number in the source file at which the method is called.</param> protected override void WrapInvoke(object caller, Action action, BusinessInvokerArgs param = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0) { Check.NotNull(action, nameof(action)); BusinessInvokerArgs bia = param ?? BusinessInvokerArgs.Default; TransactionScope txn = null; DataContextScope ctx = null; OperationType ot = ExecutionContext.Current.OperationType; try { if (bia.IncludeTransactionScope) { txn = new TransactionScope(bia.TransactionScopeOption); } ctx = DataContextScope.Begin(bia.DataContextScopeOption); action(); if (txn != null) { txn.Complete(); } } catch (Exception ex) { bia.ExceptionHandler?.Invoke(ex); throw; } finally { if (ctx != null) { ctx.Dispose(); } if (txn != null) { txn.Dispose(); } ExecutionContext.Current.OperationType = ot; } }