コード例 #1
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);
        }
コード例 #2
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();
            }
        }
コード例 #3
0
        /// <summary>
        /// 创建执行管道
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private AspectPiplineBuilder CreatPipleline(RpcContext aspectContext)
        {
            AspectPiplineBuilder aspectPipline = new AspectPiplineBuilder();

            //第一个中间件构建包装数据
            aspectPipline.Use(async(rpcContext, next) =>
            {
                await next(rpcContext);
                ResponseModel responseModel = new ResponseModel
                {
                    Data = rpcContext.ReturnValue,
                    Code = 200
                };
                await aspectContext.HttpContext.Response.WriteAsync(responseModel.ToJson());
            });
            List <RpcFilterAttribute> interceptorAttributes = GetFilterAttributes(aspectContext);

            if (interceptorAttributes.Any())
            {
                foreach (var item in interceptorAttributes)
                {
                    aspectPipline.Use(item.InvokeAsync);
                }
            }
            return(aspectPipline);
        }