Esempio n. 1
0
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            var service = (ServiceBase)context.Target;

            if (service.BalanceManagementDbContext.Database.CurrentTransaction == null)
            {
                await using var transaction = await service.BalanceManagementDbContext.Database.BeginTransactionAsync();

                try
                {
                    await context.ProceedAsync();

                    await transaction.CommitAsync();
                }
                catch
                {
                    await transaction.RollbackAsync();

                    throw;
                }
            }
            else
            {
                await context.ProceedAsync();
            }
        }
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            SnapshotProfile profile = Profile.Value;

            if (profile == null)
            {
                await context.ProceedAsync();

                return;
            }
            if (!context.HasReturnValue)
            {
                return;
            }

            string folderPath = Path.Combine(
                profile.FolderPath, context.TargetType.FullName, context.TargetMethod.Name);

            string filePath = GetFilePath(context.TargetMethod, context.Arguments, folderPath);

            if (profile.Mode == SnapshotMode.Read)
            {
                context.ReturnValue = ReadReturnTaskResult(context.TargetMethod, filePath);
            }
            else if (profile.Mode == SnapshotMode.Write)
            {
                await context.ProceedAsync();

                WriteReturnTaskResult(context.ReturnValue, context.TargetMethod, folderPath, filePath);
            }
        }
Esempio n. 3
0
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            var target = (AsyncTest)context.Target;
            await context.ProceedAsync();

            Assert.AreEqual(AsyncTest.FinalStep, target.AwaitStep);
        }
Esempio n. 4
0
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            var parameters           = context.TargetMethod.GetParameters();
            var parameterDescription = string.Join(", ",
                                                   parameters.Select(p => $"{p.ParameterType.Name} {p.Name}"));
            var signature = $"{context.Target ?? context.TargetType}.{context.TargetName}({parameterDescription})";

            using (var operation = TelemetryClient.StartOperation <DependencyTelemetry>(signature))
            {
                try
                {
                    await context.ProceedAsync();
                }
                catch (Exception)
                {
                    operation.Telemetry.Success = false;
                    throw;
                }
                finally
                {
                    operation.Telemetry.Type = Type;
                    EnrichRequestTelemetry(operation.Telemetry, context, parameters);
                }
            }
        }
Esempio n. 5
0
        public Task Advise(MethodAsyncAdviceContext context)
        {
            List <ValidationError> errors = new List <ValidationError>();

            if (ArgumentNames)
            {
                ParameterInfo[] parameters = context.TargetMethod.GetParameters();

                int i = 0;
                foreach (object argument in context.Arguments)
                {
                    errors.AddRange(argument.ValidateAnnotations(parameters[i++].Name));
                }
            }
            else
            {
                foreach (object argument in context.Arguments)
                {
                    errors.AddRange(argument.ValidateAnnotations());
                }
            }

            if (errors.Count > 0)
            {
                throw new ValidationException(errors.ToArray());
            }

            return(context.ProceedAsync());
        }
Esempio n. 6
0
        /// <summary>
        /// 拦截方法
        /// </summary>
        /// <param name="context">方法元数据</param>
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            if (context.Target is ScreenBase screenBase)
            {
                screenBase.Busy();

                await context.ProceedAsync();

                screenBase.Idle();
            }
            if (context.Target is OneActiveConductorBase oneActiveConductorBase)
            {
                oneActiveConductorBase.Busy();

                await context.ProceedAsync();

                oneActiveConductorBase.Idle();
            }
        }
Esempio n. 7
0
 public async Task Advise(MethodAsyncAdviceContext context)
 {
     using (var t = TracerFactory.StartTracer(context.TargetType, context.TargetMethod.Name))
     {
         if (info.ContainsCharacters())
         {
             t.SetAdidtionalInformation(info);
         }
         try
         {
             if (!context.IsTargetMethodAsync)
             {
                 Task.Run(() => context.ProceedAsync()).Wait();
             }
             else
             {
                 await context.ProceedAsync();
             }
         }
         catch (AggregateException aex)
         {
             if (aex.InnerExceptions == null)
             {
                 SetErrorInfo(t, aex.Flatten());
                 throw aex.Flatten();
             }
             ;
             if (aex.InnerExceptions.Count > 1)
             {
                 SetErrorInfo(t, aex.Flatten());
                 throw aex.Flatten();
             }
             var ex = aex.InnerException;
             SetErrorInfo(t, ex);
             throw ex;
         }
         catch (Exception ex)
         {
             SetErrorInfo(t, ex);
             throw;
         }
     }
 }
Esempio n. 8
0
 public async Task Advise(MethodAsyncAdviceContext context)
 {
     try
     {
         await context.ProceedAsync();
     }
     catch (CustomException)
     {
         throw new CustomException2();
     }
 }
Esempio n. 9
0
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            MethodInfo methodInfo = (MethodInfo)context.TargetMethod;
            Type       returnType = methodInfo.ReturnType.GetGenericArguments()[0];

            IApiResponse response;

            try
            {
                await context.ProceedAsync();

                return;
            }
            catch (ValidationException exception)
            {
                response = (IApiResponse)Activator.CreateInstance(returnType);
                response.ValidationErrors = exception.Errors;
            }
            catch (BusinessException exception)
            {
                response = (IApiResponse)Activator.CreateInstance(returnType);
                response.ErrorMessage = exception.Message;

                IApiError error = response as IApiError;
                if (error != null)
                {
                    error.ErrorCode = exception.Code;
                }
            }
            catch (Exception exception)
            {
                response = (IApiResponse)Activator.CreateInstance(returnType);
                response.ErrorMessage = exception.Message;

                IBusinessException businessException = exception as IBusinessException;
                if (businessException != null)
                {
                    IApiError error = response as IApiError;
                    if (error != null)
                    {
                        error.ErrorCode = businessException.Code;
                    }
                }
            }

            response.IsSuccess  = false;
            context.ReturnValue = Task.FromResult(response);
        }
Esempio n. 10
0
            public async Task Advise(MethodAsyncAdviceContext context)
            {
                await context.ProceedAsync();

                if (context.HasReturnValue)
                {
                    if (context.ReturnValue is Task)
                    {
                        context.ReturnValue = Plus(((dynamic)context.ReturnValue).Result, 1);
                    }
                    else
                    {
                        context.ReturnValue = (int)context.ReturnValue + 1;
                    }
                }
            }
Esempio n. 11
0
 public async Task Advise(MethodAsyncAdviceContext context)
 {
     //Logger initilizer here
     Console.WriteLine($"{context.TargetType.Name} started...");
     try
     {
         await context.ProceedAsync(); // this calls the original method
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     finally
     {
         Console.WriteLine($"{context.TargetType.Name} completed...");
     }
 }
 public async Task Advise(MethodAsyncAdviceContext context)
 {
     try
     {
         await context.ProceedAsync();
     }
     catch (Exception ex)
     {
         MethodExecutionArgs args = new MethodExecutionArgs(context, ex);
         OnException(args);
         if (args.FlowBehavior == FlowBehavior.RethrowException)
         {
             throw;
         }
         if (args.FlowBehavior == FlowBehavior.ThrowException)
         {
             throw ex;
         }
     }
 }
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            MethodExecutionArgs args = new MethodExecutionArgs(context, null);

            try
            {
                OnEntry(args);
                await context.ProceedAsync();

                OnSuccess(args);
            }
            catch (Exception ex)
            {
                args.SetException(ex);
                OnException(args);
            }
            finally
            {
                OnExit(args);
            }
        }
Esempio n. 14
0
 public async Task Advise(MethodAsyncAdviceContext context)
 {
     using (var t = TracerFactory.StartTracer(context.TargetType, context.TargetMethod.Name))
     {
         if (info.ContainsCharacters()) t.SetAdidtionalInformation(info);
         try
         {
             if (!context.IsTargetMethodAsync) Task.Run(() => context.ProceedAsync()).Wait();
             else await context.ProceedAsync();
         }
         catch (AggregateException aex)
         {
             if (aex.InnerExceptions == null)
             {
                 SetErrorInfo(t, aex.Flatten());
                 throw aex.Flatten();
             };
             if (aex.InnerExceptions.Count > 1)
             {
                 SetErrorInfo(t, aex.Flatten());
                 throw aex.Flatten();
             }
             var ex = aex.InnerException;
             SetErrorInfo(t, ex);
             throw ex;
         }
         catch (Exception ex)
         {
             SetErrorInfo(t, ex);
             throw;
         }
     }
 }
Esempio n. 15
0
 public Task Advise(MethodAsyncAdviceContext context)
 {
     return(context.ProceedAsync());
 }
Esempio n. 16
0
 public async Task Advise(MethodAsyncAdviceContext context)
 {
     await context.ProceedAsync();
 }
Esempio n. 17
0
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            await context.ProceedAsync();

            context.ReturnValue = Plus(((dynamic)context.ReturnValue).Result, 1);
        }
Esempio n. 18
0
            public async Task Advise(MethodAsyncAdviceContext context)
            {
                await context.ProceedAsync();

                Console.WriteLine("Success!");
            }