Ejemplo n.º 1
0
 public TracingDiagnosticMethod(ITracingDiagnosticProcessor TracingDiagnosticProcessor, MethodInfo MethodInfo, String DiagnosticName)
 {
     _TracingDiagnosticProcessor = TracingDiagnosticProcessor;
     _Reflector          = MethodInfo.GetReflector();
     _DiagnosticName     = DiagnosticName;
     _RarameterResolvers = GetParameterResolvers(MethodInfo).ToArray();
 }
        protected virtual Task BeforeAsync(MethodReflector targetMethod, object[] args)
        {
#if DEBUG
            IsBefore = true;
#endif
            return(Task.CompletedTask);
        }
Ejemplo n.º 3
0
        protected virtual Task BeforeAsync(MethodReflector targetMethod, object[] args)
        {
#if NETSTANDARD
            return(Task.CompletedTask);
#else
            return(Task.FromResult <object>(null));
#endif
        }
 public TracingDiagnosticMethod(ITracingDiagnosticProcessor tracingDiagnosticProcessor, MethodInfo method,
                                string diagnosticName)
 {
     _tracingDiagnosticProcessor = tracingDiagnosticProcessor;
     _reflector          = method.GetReflector();
     _diagnosticName     = diagnosticName;
     _parameterResolvers = GetParameterResolvers(method).ToArray();
 }
 public DiagnosticMethodSubscription(IDiagnosticProcessor diagnosticProcessor, MethodInfo method,
                                     string diagnosticName)
 {
     _DiagnosticProcessor = diagnosticProcessor;
     _reflector           = method.GetReflector();
     _diagnosticName      = diagnosticName;
     _parameterResolvers  = GetParameterResolvers(method).ToArray();
 }
Ejemplo n.º 6
0
        public virtual Task AfterAsync(MethodReflector method, Exception exception, object instance, params object[] param)
        {
            Console.WriteLine($"AfterAsync Name:{method.Name}");
#if NETSTANDARD
            return(Task.CompletedTask);
#else
            return(Task.FromResult <object>(null));
#endif
        }
Ejemplo n.º 7
0
        private Expression GetEntitiesAsParameterExpr(Type entityType)
        {
            var getEntitiesMi  = MethodReflector.Get <EntityManager, Type>((em, t) => em.GetEntities(t));
            var entityTypeExpr = Expression.Constant(entityType, typeof(Type));
            var egExpr         = Expression.Call(_entityManagerParameterExpr, getEntitiesMi, entityTypeExpr);

            var methExpr = Expression.Call(typeof(Enumerable), "Cast", new Type[] { entityType }, egExpr);

            return(methExpr);
        }
Ejemplo n.º 8
0
        public async Task <object> ExecuteAsync(MethodReflector targetMethod, object instance, object[] args)
        {
            object result = null;

            await BeforeAsync(targetMethod, args);

            result = targetMethod.Invoke(instance, args);

            await AfterAsync(targetMethod, args);

            return(result);
        }
Ejemplo n.º 9
0
 private static HttpResponseMessageSettingsAttribute[] GetResponseSetters(MethodReflector mr, TypeReflector tr)
 {
     return(mr.GetCustomAttributes <HttpResponseMessageSettingsAttribute>()
            .Union(mr.Parameters.SelectMany(i => i.GetCustomAttributes <ParameterResponseMessageSettingsAttribute>()
                                            .Select(j =>
     {
         j.Parameter = i.MemberInfo;
         return j;
     })))
            .Union(tr.GetCustomAttributes <HttpResponseMessageSettingsAttribute>())
            .OrderBy(i => i.Order)
            .ToArray());
 }
Ejemplo n.º 10
0
        public MethodReflectorBenchmarks()
        {
            var typeInfo = typeof(MethodFakes).GetTypeInfo();

            _method                         = typeInfo.GetMethod("Call");
            _staticMethod                   = typeInfo.GetMethod("StaticCall");
            _virtualMethod                  = typeInfo.GetMethod("CallVirt");
            _staticReflector                = _staticMethod.GetReflector();
            _callReflectorWithCallOp        = _method.GetReflector(CallOptions.Call);
            _callReflectorWithCallVirtOp    = _method.GetReflector(CallOptions.Callvirt);
            _virtualReflectorWithCallOp     = _virtualMethod.GetReflector(CallOptions.Call);
            _virtualReflectorWithCallVirtOp = _virtualMethod.GetReflector(CallOptions.Callvirt);
            _instance                       = new MethodFakes();
        }
Ejemplo n.º 11
0
 private static IHttpRequestMessageSettings[] GetRequestSetters(MethodReflector mr, TypeReflector tr)
 {
     return(mr.GetCustomAttributesDistinctBy <HttpMethodAttribute>(tr)
            .Select(i => i.CreateSettings(mr.Parameters.Where(j => j.IsDefined <RouteAttribute>()), mr.Parameters.Where(j => j.IsDefined <QueryAttribute>())))
            .First()
            .Union(mr.Parameters.SelectMany(i => i.GetCustomAttributes <ParameterRequestMessageSettingsAttribute>()
                                            .Select(j =>
     {
         j.Parameter = i.MemberInfo;
         return j;
     })))
            .Union(tr.GetCustomAttributes <HttpRequestMessageSettingsAttribute>())
            .Union(mr.GetCustomAttributes <HttpRequestMessageSettingsAttribute>())
            .OrderBy(i => i.Order)
            .ToArray());
 }
Ejemplo n.º 12
0
        private ServiceEntry Create(MethodInfo method, MethodReflector implementationMethod)
        {
            var serviceId = _serviceIdGenerator.GenerateServiceId(method);

            var serviceDescriptor = new ServiceDescriptor
            {
                Id = serviceId
            };

            var descriptorAttributes = method.GetCustomAttributes <RpcServiceDescriptorAttribute>();

            foreach (var descriptorAttribute in descriptorAttributes)
            {
                descriptorAttribute.Apply(serviceDescriptor);
            }
            return(new ServiceEntry
            {
                Descriptor = serviceDescriptor,
                Func = parameters =>
                {
                    var serviceScopeFactory = _serviceProvider.GetRequiredService <IServiceScopeFactory>();
                    using (var scope = serviceScopeFactory.CreateScope())
                    {
                        var instance = scope.ServiceProvider.GetRequiredService(method.DeclaringType);

                        var list = new List <object>();
                        foreach (var parameterInfo in method.GetParameters())
                        {
                            if (parameterInfo.HasDefaultValue && !parameters.ContainsKey(parameterInfo.Name))
                            {
                                list.Add(parameterInfo.DefaultValue);
                                continue;
                            }
                            var value = parameters[parameterInfo.Name];
                            var parameterType = parameterInfo.ParameterType;

                            var parameter = _typeConvertibleService.Convert(value, parameterType);
                            list.Add(parameter);
                        }

                        var result = implementationMethod.Invoke(instance, list.ToArray());

                        return Task.FromResult(result);
                    }
                }
            });
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 调用切面的拦截方法
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="methodInfo"></param>
        /// <param name="parameters"></param>
        /// <param name="context"></param>
        /// <param name="invocation"></param>
        /// <param name="_next"></param>
        /// <param name="returnValue"></param>
        /// <param name="returnParam"></param>
        /// <param name="injectAnotation"></param>
        /// <returns></returns>
        public static object InvokeInstanceMethod(object instance, MethodReflector methodInfo, ParameterInfo[] parameters, IComponentContext context,
                                                  AspectContext invocation = null, AspectDelegate _next = null, object returnValue = null, string returnParam = null, Attribute injectAnotation = null)
        {
            if (parameters == null || parameters.Length == 0)
            {
                return(methodInfo.Invoke(instance, null));
            }

            //自动类型注入

            List <object> parameterObj = new List <object>();

            foreach (var parameter in parameters)
            {
                if (invocation != null && parameter.ParameterType == typeof(AspectContext))
                {
                    parameterObj.Add(invocation);
                    continue;
                }
                if (_next != null && parameter.ParameterType == typeof(AspectDelegate))
                {
                    parameterObj.Add(_next);
                    continue;
                }

                if (injectAnotation != null && parameter.ParameterType == injectAnotation.GetType())
                {
                    parameterObj.Add(injectAnotation);
                    continue;
                }

                if (returnValue != null && !string.IsNullOrWhiteSpace(returnParam) && parameter.Name.Equals(returnParam))
                {
                    //如果指定的类型会出错
                    parameterObj.Add(returnValue);
                    continue;
                }

                var autowired = parameter.GetCustomAttribute <Autowired>();
                if (autowired != null)
                {
                    parameterObj.Add(autowired.ResolveParameter(parameter, context));
                    continue;
                }

                var value = parameter.GetCustomAttribute <Value>();
                if (value != null)
                {
                    parameterObj.Add(value.ResolveParameter(parameter, context));
                    continue;
                }

                if (parameter.HasDefaultValue)
                {
                    parameterObj.Add(parameter.RawDefaultValue);
                    continue;
                }

                if (parameter.IsOptional)
                {
                    parameterObj.Add(Type.Missing);
                    continue;
                }

                if (parameter.IsOut)
                {
                    parameterObj.Add(Type.Missing);
                    continue;
                }

                if (parameter.ParameterType.IsValueType || parameter.ParameterType.IsEnum)
                {
                    parameterObj.Add(parameter.RawDefaultValue);
                    continue;
                }


                //如果拿不到就默认
                context.TryResolve(parameter.ParameterType, out var obj);
                parameterObj.Add(obj);
            }

            return(methodInfo.Invoke(instance, parameterObj.ToArray()));
        }
Ejemplo n.º 14
0
 public DiagnosticEvent(IDiagnosticProcessor diagnosticProcessor, MethodInfo method)
 {
     _diagnosticProcessor = diagnosticProcessor;
     _reflector           = method.GetReflector();
     _parameterResolvers  = GetParameterResolvers(method).ToArray();
 }
Ejemplo n.º 15
0
 public override IAsyncPolicy BuildAsync(MethodReflector method)
 {
     return(Policy.TimeoutAsync(timeSpan, TimeoutStrategy.Optimistic));
 }
Ejemplo n.º 16
0
 public override IAsyncPolicy BuildAsync(MethodReflector method)
 {
     return(Policy.BulkheadAsync(maxParallelization, maxQueuingActions));
 }
Ejemplo n.º 17
0
 public override IAsyncPolicy BuildAsync(MethodReflector method)
 {
     return(PolicyExtensions.CreatePolicyBuilder(ExceptionType)
            .AdvancedCircuitBreakerAsync(failureThreshold, samplingDuration, minimumThroughput, durationOfBreak));
 }
Ejemplo n.º 18
0
 public virtual void After(MethodReflector method, Exception exception, object instance, params object[] param)
 {
     Console.WriteLine($"After Name:{method.Name}");
 }
Ejemplo n.º 19
0
 public override bool CanAspect(MethodReflector method)
 {
     return(method.IsDefined <HttpMethodAttribute>());
 }
Ejemplo n.º 20
0
 public override bool CanAspect(MethodReflector method)
 {
     return(method.Parameters.Any(i => i.IsDefined <InjectAttribute>()));
 }
Ejemplo n.º 21
0
 public override bool CanAspect(MethodReflector method)
 {
     return(method.IsDefined <AbstractPolicyAttribute>());
 }
Ejemplo n.º 22
0
 public abstract ISyncPolicy Build(MethodReflector method);
        protected override Task AfterAsync(MethodReflector targetMethod, object[] args)
        {
            Console.WriteLine($"AfterAsync {targetMethod.Name}");

            return(base.AfterAsync(targetMethod, args));
        }
Ejemplo n.º 24
0
 public abstract IAsyncPolicy BuildAsync(MethodReflector method);
Ejemplo n.º 25
0
 public virtual Task BeforeAsync(MethodReflector method, object instance, params object[] param)
 {
     Console.WriteLine($"BeforeAsync Name:{method.Name}");
     return(Task.CompletedTask);
 }
Ejemplo n.º 26
0
 public virtual Task AfterAsync(MethodReflector method, Exception exception, object instance, params object[] param)
 {
     Console.WriteLine($"AfterAsync Name:{method.Name}");
     return(Task.CompletedTask);
 }
Ejemplo n.º 27
0
 public override IAsyncPolicy BuildAsync(MethodReflector method)
 {
     return(PolicyExtensions.CreatePolicyBuilder(ExceptionType).RetryAsync(retryCount));
 }
Ejemplo n.º 28
0
 public override bool CanAspect(MethodReflector method)
 {
     return(method.IsDefined <CacheAttribute>());  // 限制只对有缓存定义的方法起效
 }
Ejemplo n.º 29
0
 public override IAsyncPolicy BuildAsync(MethodReflector method)
 {
     return(PolicyExtensions.CreatePolicyBuilder(ExceptionType)
            .CircuitBreakerAsync(exceptionsAllowedBeforeBreaking, durationOfBreak));
 }
Ejemplo n.º 30
0
 public virtual void Before(MethodReflector method, object instance, params object[] param)
 {
     Console.WriteLine($"Before Name:{method.Name}");
 }