예제 #1
0
 public Task <TResult> InvokeTask <TResult>(AspectActivatorContext activatorContext)
 {
     using (var context = _aspectContextFactory.CreateContext(activatorContext))
     {
         var aspectBuilder = _aspectBuilderFactory.Create(context);
         var invoke        = aspectBuilder.Build()(context);
         if (invoke.IsFaulted)
         {
             var innerException = invoke.Exception?.InnerException;
             throw context.InvocationException(innerException);
         }
         if (!invoke.IsCompleted)
         {
             invoke.GetAwaiter().GetResult();
         }
         var result = context.ReturnValue;
         if (result is Task <TResult> resultTask)
         {
             return(resultTask);
         }
         else if (result is Task task)
         {
             if (!task.IsCompleted)
             {
                 task.GetAwaiter().GetResult();
             }
             return(TaskUtils <TResult> .CompletedTask);
         }
         else
         {
             throw context.InvocationException(new InvalidCastException($"Unable to cast object of type '{result.GetType()}' to type '{typeof(Task<TResult>)}'."));
         }
     }
 }
        public async Task <TResult> InvokeTask <TResult>(AspectActivatorContext activatorContext)
        {
            var context = _aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = _aspectBuilderFactory.Create(context);
                await aspectBuilder.Build()(context);

                var result = context.ReturnValue;
                if (result is Task <TResult> taskWithResult)
                {
                    return(await taskWithResult);
                }
                else if (result is Task task)
                {
                    await task;
                    return(default(TResult));
                }
                else
                {
                    throw _aspectExceptionWrapper.Wrap(context, new InvalidCastException(
                                                           $"Unable to cast object of type '{result.GetType()}' to type '{typeof(Task<TResult>)}'."));
                }
            }
            catch (Exception ex)
            {
                throw _aspectExceptionWrapper.Wrap(context, ex);
            }
            finally
            {
                _aspectContextFactory.ReleaseContext(context);
            }
        }
        public TResult Invoke <TResult>(AspectActivatorContext activatorContext)
        {
            var context = _aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = _aspectBuilderFactory.Create(context);
                var task          = aspectBuilder.Build()(context);
                if (task.IsFaulted)
                {
                    throw context.InvocationException(task.Exception);
                }
                else if (!task.IsCompleted)
                {
                    task.GetAwaiter().GetResult();
                }
                return((TResult)context.ReturnValue);
            }
            catch (Exception ex)
            {
                throw context.InvocationException(ex);
            }
            finally
            {
                _aspectContextFactory.ReleaseContext(context);
            }
        }
        public TResult Invoke <TResult>(AspectActivatorContext activatorContext)
        {
            var context = _aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = _aspectBuilderFactory.Create(context);
                var task          = aspectBuilder.Build()(context);
                if (task.IsFaulted)
                {
                    throw _aspectExceptionWrapper.Wrap(context, task.Exception.InnerException);
                }
                if (!task.IsCompleted)
                {
                    // try to avoid potential deadlocks.
                    NoSyncContextScope.Run(task);
                    // task.GetAwaiter().GetResult();
                }

                return((TResult)context.ReturnValue);
            }
            catch (Exception ex)
            {
                throw _aspectExceptionWrapper.Wrap(context, ex);
            }
            finally
            {
                _aspectContextFactory.ReleaseContext(context);
            }
        }
예제 #5
0
 public virtual AspectContext CreateContext(AspectActivatorContext activatorContext)
 {
     return(new RuntimeAspectContext(_serviceProvider,
                                     activatorContext.ServiceMethod,
                                     activatorContext.TargetMethod,
                                     activatorContext.ProxyMethod,
                                     activatorContext.TargetInstance,
                                     activatorContext.ProxyInstance,
                                     activatorContext.Parameters ?? emptyParameters));
 }
예제 #6
0
 public ValueTask <TResult> InvokeValueTask <TResult>(AspectActivatorContext activatorContext)
 {
     using (var context = _aspectContextFactory.CreateContext(activatorContext))
     {
         var aspectBuilder = _aspectBuilderFactory.Create(context);
         var invoke        = aspectBuilder.Build()(context);
         if (invoke.IsFaulted)
         {
             var innerException = invoke.Exception?.InnerException;
             throw context.InvocationException(innerException);
         }
         if (!invoke.IsCompleted)
         {
             invoke.GetAwaiter().GetResult();
         }
         return((ValueTask <TResult>)context.ReturnValue);
     }
 }
예제 #7
0
        public async ValueTask <TResult> InvokeValueTask <TResult>(AspectActivatorContext activatorContext)
        {
            var context = _aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = _aspectBuilderFactory.Create(context);
                var invoke        = aspectBuilder.Build()(context);

                if (invoke.IsFaulted)
                {
                    throw _aspectExceptionWrapper.Wrap(context, invoke.Exception?.InnerException);
                }

                if (!invoke.IsCompleted)
                {
                    await invoke;
                }

                switch (context.ReturnValue)
                {
                case null:
                    return(default(TResult));

                case ValueTask <TResult> taskWithResult:
                    return(taskWithResult.Result);

                case ValueTask task:
                    return(default(TResult));

                default:
                    throw _aspectExceptionWrapper.Wrap(context, new InvalidCastException(
                                                           $"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(ValueTask<TResult>)}'."));
                }
            }
            catch (Exception ex)
            {
                throw _aspectExceptionWrapper.Wrap(context, ex);
            }
            finally
            {
                _aspectContextFactory.ReleaseContext(context);
            }
        }
        public async ValueTask <TResult> InvokeValueTask <TResult>(AspectActivatorContext activatorContext)
        {
            var context = _aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = _aspectBuilderFactory.Create(context);
                await aspectBuilder.Build()(context);

                return(await(ValueTask <TResult>) context.ReturnValue);
            }
            catch (Exception ex)
            {
                throw _aspectExceptionWrapper.Wrap(context, ex);
            }
            finally
            {
                _aspectContextFactory.ReleaseContext(context);
            }
        }
예제 #9
0
        public ValueTask <TResult> InvokeValueTask <TResult>(AspectActivatorContext activatorContext)
        {
            var context = _aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = _aspectBuilderFactory.Create(context);
                var invoke        = aspectBuilder.Build()(context);
                if (invoke.IsFaulted)
                {
                    var innerException = invoke.Exception?.InnerException;
                    throw context.InvocationException(innerException);
                }

                return((ValueTask <TResult>)context.ReturnValue);
            }
            finally
            {
                _aspectContextFactory.ReleaseContext(context);
            }
        }
예제 #10
0
        public Task <TResult> InvokeTask <TResult>(AspectActivatorContext activatorContext)
        {
            var context = _aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = _aspectBuilderFactory.Create(context);
                var invoke        = aspectBuilder.Build()(context);
                if (invoke.IsFaulted)
                {
                    var innerException = invoke.Exception?.InnerException;
                    throw context.InvocationException(innerException);
                }

                var result = context.ReturnValue;
                if (result == null)
                {
                    return(default(Task <TResult>));
                }
                else if (result is Task <TResult> resultTask)
                {
                    return(resultTask);
                }
                else if (result is Task task)
                {
                    return(TaskUtils <TResult> .CompletedTask);
                }
                else
                {
                    throw context.InvocationException(new InvalidCastException($"Unable to cast object of type '{result.GetType()}' to type '{typeof(Task<TResult>)}'."));
                }
            }
            finally
            {
                _aspectContextFactory.ReleaseContext(context);
            }
        }
예제 #11
0
        public TResult Invoke <TResult>(AspectActivatorContext activatorContext)
        {
            var context = _aspectContextFactory.CreateContext(activatorContext);

            try
            {
                var aspectBuilder = _aspectBuilderFactory.Create(context);
                var invoke        = aspectBuilder.Build()(context);
                if (invoke.IsFaulted)
                {
                    var innerException = invoke.Exception?.InnerException;
                    throw context.InvocationException(innerException);
                }
                if (!invoke.IsCompleted)
                {
                    invoke.GetAwaiter().GetResult();
                }
                return((TResult)context.ReturnValue);
            }
            finally
            {
                _aspectContextFactory.ReleaseContext(context);
            }
        }