Ejemplo n.º 1
0
        /// <summary>
        /// 创建委托
        /// </summary>
        /// <param name="service">继承NarutoWebSocketService的服务</param>
        /// <param name="action">执行的方法</param>
        /// <param name="isParameter">是否为带参数的方法</param>
        /// <param name="parameterType">参数类型</param>
        /// <param name="parameterEntity">方法的参数</param>
        /// <returns></returns>
        private Task Create(object service, string action, bool isParameter, Type parameterType, object parameterEntity)
        {
            //定义输入参数
            var p1 = Expression.Parameter(service.GetType(), "service");
            //方法的参数对象
            var methodParameter = Expression.Parameter(!isParameter ? typeof(object) : parameterType, "methodParameter");

            //动态执行方法
            var methods = methodCache.Get(service.GetType(), action);

            if (methods.method == null)
            {
                return(default);
Ejemplo n.º 2
0
        public JsonRpcResponse Handle(JsonRpcRequest request)
        {
            try
            {
                // Get method
                var method = _methodCache.Get(request.MethodName);
                if (method == null)
                {
                    return(new JsonRpcErrorResponse(request.Id, -32601, "Method not found"));
                }

                // Find resolver
                var resolver = _parameterResolvers.FirstOrDefault(r => r.CanResolve(request.Parameters));
                if (resolver == null)
                {
                    return(new JsonRpcErrorResponse(request.Id, -32602, "Invalid params"));
                }

                // Resolve parameters
                var parameters = resolver.Resolve(request.Parameters, method.Parameters);

                // Execute method with resolved parameters
                var result = method.Execute(parameters);

                // Return response
                return(new JsonRpcSuccessResponse(request.Id, result));
            }
            catch (Exception exception)
            {
                if (exception.InnerException is JsonRpcException)
                {
                    var inner = exception.InnerException as JsonRpcException;

                    var message = "Internal error";
                    if (!string.IsNullOrEmpty(inner.Message))
                    {
                        message = inner.Message;
                    }

                    return(new JsonRpcErrorResponse(request.Id, inner.ErrorCode, message, inner.ToString()));
                }

                return(new JsonRpcErrorResponse(request.Id, -32603, "Internal error", exception.ToString()));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 开始处理订阅所对应的方法
        /// </summary>
        /// <param name="subscribeName"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public async Task HandlerAsync(string subscribeName, string msg)
        {
            subscribeName.CheckNullOrEmpty();

            logger.LogInformation("开始处理接收到的订阅信息,subscribeName:{subscribeName},msg:{msg}", subscribeName, msg);
            //查找当前订阅信息对应的 所处的对象信息
            BaseSubscribeTypeModel baseSubscribeType = SubscribeTypeFactory.Get(subscribeName);

            if (baseSubscribeType == null)
            {
                logger.LogWarning("查找不到指定的订阅信息:channel:{channel}", subscribeName);
                return;
            }

            //根据类型获取泛型
            var dynamicMethod = serviceProvider.GetRequiredService(typeof(DynamicMethodExpression <>).MakeGenericType(baseSubscribeType.ServiceType)) as IDynamicMethodExpression;

            //获取当前执行方法的参数
            var methods = methodCache.Get(baseSubscribeType.ServiceType, baseSubscribeType.MethodName);

            if (methods.method == null)
            {
                return;
            }
            var paramters = methods.parameterInfos;

            //获取当前方法是否含有参数
            var isParamter = paramters != null && paramters.Count() > 0;
            //验证是否是有参的方法,当前默认只支持一个参数
            var paramtersObject = isParamter ? msg.ToDeserialized(paramters[0].ParameterType) : null;

            //消息处理前的aop事件
            NarutoMessageAopEvent.PreHandler?.Invoke(subscribeName, msg);

            //操作动态执行方法接口
            await dynamicMethod.ExecAsync(serviceProvider.GetRequiredService(baseSubscribeType.ServiceType), baseSubscribeType.MethodName, isParamter, isParamter?paramters[0].ParameterType : null, paramtersObject);

            //消息处理后的aop事件
            NarutoMessageAopEvent.AfterHandler?.Invoke(subscribeName, msg);

            logger.LogInformation("处理完毕接受到的订阅信息");
        }