Ejemplo n.º 1
0
        /// <summary>
        /// 执行
        /// </summary>
        public override async Task Invoke(AspectCore.DynamicProxy.AspectContext context, AspectCore.DynamicProxy.AspectDelegate next)
        {
            TransactionOptions transactionOptions = new TransactionOptions();

            // 设置事务隔离级别
            transactionOptions.IsolationLevel = IsolationLevel;
            // 设置事务超时时间为60秒
            transactionOptions.Timeout = new TimeSpan(0, 0, Timeout);
            using (TransactionScope scope = new TransactionScope(ScopeOption, transactionOptions))
            {
                try
                {
                    // 实现事务性工作
                    await next(context);

                    var manager = context.ServiceProvider.GetService(typeof(IUnitOfWorkManager)) as IUnitOfWorkManager;
                    manager?.Commit();
                    scope.Complete();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            //await next(context);
        }
 public static async Task AwaitIfAsync(this AspectContext aspectContext, object returnValue)
 {
     if (returnValue == null)
     {
         return;
     }
     if (returnValue is Task task)
     {
         try
         {
             await task;
         }
         catch (Exception ex)
         {
             throw aspectContext.InvocationException(ex);
         }
     }
 }
Ejemplo n.º 3
0
        public static bool IsAsync(this AspectContext aspectContext)
        {
            if (aspectContext == null)
            {
                throw new ArgumentNullException(nameof(aspectContext));
            }
            var isAsyncFromMetaData = isAsyncCache.GetOrAdd(aspectContext.ServiceMethod, IsAsyncFromMetaData);

            if (isAsyncFromMetaData)
            {
                return(true);
            }
            if (aspectContext.ReturnValue != null)
            {
                return(IsAsyncType(aspectContext.ReturnValue.GetType().GetTypeInfo()));
            }
            return(false);
        }
Ejemplo n.º 4
0
 public static void AwaitIfAsync(this AspectContext aspectContext, object returnValue)
 {
     if (returnValue == null)
     {
         return;
     }
     if (returnValue is Task task)
     {
         if (task.IsFaulted)
         {
             var innerException = task.Exception?.InnerException;
             throw aspectContext.InvocationException(innerException);
         }
         if (!task.IsCompleted)
         {
             task.GetAwaiter().GetResult();
         }
     }
 }
Ejemplo n.º 5
0
        public static object UnwrapAsyncReturnValue(this AspectContext aspectContext)
        {
            if (aspectContext == null)
            {
                throw new ArgumentNullException(nameof(aspectContext));
            }
            if (!aspectContext.IsAsync())
            {
                throw new AspectInvocationException(aspectContext, new InvalidOperationException("This operation only support asynchronous method."));
            }
            var returnValue = aspectContext.ReturnValue;

            if (returnValue == null)
            {
                return(null);
            }
            var returnTypeInfo = returnValue.GetType().GetTypeInfo();

            return(Unwrap(returnValue, returnTypeInfo));
        }
        public static async ValueTask AwaitIfAsync(this AspectContext aspectContext, object returnValue)
        {
            switch (returnValue)
            {
            case null:
                break;

            case Task task:
                await task;
                break;

            case ValueTask valueTask:
                await valueTask;
                break;

            default:
                if (returnValue.GetType().GetTypeInfo().IsValueTaskWithResult())
                {
                    await(dynamic) returnValue;
                }
                break;
            }
        }
Ejemplo n.º 7
0
 public static async Task <T> UnwrapAsyncReturnValue <T>(this AspectContext aspectContext)
 {
     return((T) await UnwrapAsyncReturnValue(aspectContext));
 }
Ejemplo n.º 8
0
 public static ValueTask AwaitIfAsync(this AspectContext aspectContext)
 {
     return(AwaitIfAsync(aspectContext, aspectContext.ReturnValue));
 }
Ejemplo n.º 9
0
 public override Task Invoke(AspectCore.DynamicProxy.AspectContext context, AspectCore.DynamicProxy.AspectDelegate next)
 {
     this.action?.Invoke(context.ImplementationMethod?.Name);
     return(context.Invoke(next));
 }
 public void ReleaseContext(AspectContext aspectContext)
 {
     (aspectContext as IDisposable)?.Dispose();
 }
 public abstract Task Invoke(AspectContext context, AspectDelegate next);
 public AspectInvocationException(AspectContext aspectContext, Exception innerException)
     : base($"Exception has been thrown by the aspect of an invocation. ---> {innerException?.Message}.", innerException)
 {
     AspectContext = aspectContext;
 }
 public Task Invoke(AspectContext context, AspectDelegate next)
 {
     return(_aspectDelegate(next)(context));
 }