コード例 #1
0
        public void Intercept(IInvocation invocation)
        {
            var methondInterceptorAttributes = _methodFilters.GetOrAdd($"{invocation.TargetType.FullName}#{invocation.MethodInvocationTarget.Name}", key => {
                var methondAttributes = invocation.MethodInvocationTarget.GetCustomAttributes(true)
                                        .Where(i => typeof(AbstractInterceptorAttribute).IsAssignableFrom(i.GetType()))
                                        .Cast <AbstractInterceptorAttribute>().ToList();
                var classInterceptorAttributes = invocation.TargetType.GetCustomAttributes(true)
                                                 .Where(i => typeof(AbstractInterceptorAttribute).IsAssignableFrom(i.GetType()))
                                                 .Cast <AbstractInterceptorAttribute>();
                methondAttributes.AddRange(classInterceptorAttributes);
                return(methondAttributes);
            });

            PropertyInject.PropertiesInject(_serviceProvider, methondInterceptorAttributes);

            if (methondInterceptorAttributes.Any())
            {
                AspectPiplineBuilder aspectPipline = new AspectPiplineBuilder();
                foreach (var item in methondInterceptorAttributes)
                {
                    aspectPipline.Use(item.InvokeAsync);
                }

                AspectContext aspectContext  = new DefaultAspectContext(invocation);
                var           aspectDelegate = aspectPipline.Build(context => {
                    invocation.Proceed();
                    aspectContext.ReturnValue = invocation.ReturnValue;
                    return(Task.CompletedTask);
                });
                aspectDelegate.Invoke(aspectContext).GetAwaiter().GetResult();
            }
        }
コード例 #2
0
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <returns></returns>
        private async Task HandleRequest(HttpContext context, RequestModel requestModel)
        {
            Type serviceType     = _types[requestModel.TypeFullName];
            var  instance        = context.RequestServices.GetService(serviceType);
            var  instanceType    = instance.GetType();
            var  method          = instanceType.GetMethod(requestModel.MethodName);
            var  methodParamters = method.GetParameters();
            var  paramters       = requestModel.Paramters;

            for (int i = 0; i < paramters.Length; i++)
            {
                if (paramters[i].GetType() != methodParamters[i].ParameterType)
                {
                    paramters[i] = paramters[i].ToJson().FromJson(methodParamters[i].ParameterType);
                }
            }
            RpcContext aspectContext = new RpcContext
            {
                Parameters  = paramters,
                HttpContext = context,
                TargetType  = instanceType,
                Method      = method
            };
            AspectPiplineBuilder aspectPipline      = CreatPipleline(aspectContext);
            RpcRequestDelegate   rpcRequestDelegate = aspectPipline.Build(PiplineEndPoint(instance, aspectContext));

            await rpcRequestDelegate(aspectContext);
        }