public override void GetMathsInfo(Type t)
        {
            this.clear();


            try
            {
                IConnection conection = factory.CreateConnection();
                #region 方法二
                MethodInfo[] info       = t.GetMethods();
                List <Type>  interfaces = t.GetInterfaces().Where(type => (!typeof(MsgController).IsAssignableFrom(type))).ToList();
                if (interfaces.Count > 1 || interfaces.Count == 0)
                {
                    throw new Exception("MsgController的实现类必须继承Sharing中共享的接口," +
                                        "且不能继承MsgController外的其他接口,且不能多层继承");
                }

                for (int k = 0; k < interfaces.Count; k++)
                {
                    ///获取接口和类信息
                    this.ControllerObj     = Activator.CreateInstance(t);
                    this.packageName       = t.Namespace;
                    this.FullName          = t.FullName;
                    this.interfaceFullName = interfaces[k].FullName;

                    IEnumerable <CustomAttributeData> eFRpcServiceAttribute = t.CustomAttributes.Where(type => type.AttributeType == typeof(EFRpcServiceAttribute)).ToList();
                    foreach (var a in eFRpcServiceAttribute)
                    {
                        foreach (var kv in a.NamedArguments)
                        {
                            if (kv.MemberName.Equals("version"))
                            {
                                this.version = kv.TypedValue.Value.ToString();
                            }
                        }
                    }

                    for (int i = 0; i < info.Length; i++)
                    {
                        MsgFun     mf = new MsgFun();
                        MethodInfo md = info[i];

                        //方法名
                        string mothodName = md.Name;
                        Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name}");
                        //参数集合
                        ParameterInfo[] paramInfos = md.GetParameters();
                        if (md.Name.Equals("ToString") || md.Name.Equals("Equals") || md.Name.Equals("GetHashCode") || md.Name.Equals("GetType"))
                        {
                            continue;
                        }
                        MsgFun mfs = new MsgFun();
                        mfs.Name       = md.Name;
                        mfs.rep        = md.ReturnType;
                        mfs.methodInfo = md;
                        this.put(md.Name, mfs);
                        for (int j = 0; j < paramInfos.Length; j++)
                        {
                            ParameterInfo parameterInfo = paramInfos[j];
                            mfs.req = parameterInfo.ParameterType;
                            Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name},{"入参" + j + ":" + parameterInfo.ParameterType}");
                        }
                        IModel channel = conection.CreateModel();
                        channel.QueueDeclare(queue: this.version + this.interfaceFullName + "." + md.Name, durable: false,
                                             exclusive: false, autoDelete: false, arguments: null);
                        var consumer = new EventingBasicConsumer(channel);
                        Console.WriteLine("[*] Waiting for message.");
                        consumer.Received += (model, ea) =>
                        {
                            var properties     = ea.BasicProperties;
                            var replyProerties = channel.CreateBasicProperties();
                            replyProerties.CorrelationId = properties.CorrelationId;

                            SuperMsg superMsg = this.serializer.DeSerializeString <SuperMsg>(ea.Body.ToString());
                            object[] objs     = new object[] { superMsg.msg };// new object[] { JsonSerializer.CreateDefault().Deserialize( ea.Body.ToArray().ToString(), parameterInfo.ParameterType) };//new object[] { ProtobufSerializer.DeSerializeBytes(parameterInfo.ParameterType, ea.Body.ToArray()) }
                            object   rep      = mfs.methodInfo.Invoke(this.ControllerObj, objs);
                            if (md.ReturnType != typeof(void))
                            {
                                channel.BasicPublish(exchange: "", routingKey: properties.ReplyTo,
                                                     basicProperties: replyProerties, body: this.serializer.SerializeBytes(superMsg.setMsg(rep)));//ProtobufSerializer.SerializeBytes(mfs.rep,rep)
                            }
                            channel.BasicAck(ea.DeliveryTag, false);
                            // Console.WriteLine($"Return result: {"消息:" + message}");
                        };
                        channel.BasicConsume(queue: this.version + this.interfaceFullName + "." + md.Name, autoAck: false, consumer: consumer);
                        Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name},{"返回值" + ":" + md.ReturnType}");
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#2
0
        public override void GetMathsInfo <T>()
        {
            this.clear();
            //传递参数
            var t = typeof(T);
            //var t = Type.GetType(className);

            List <Type> interfaces = t.GetInterfaces().Where(type => (!typeof(MsgController).IsAssignableFrom(type))).ToList();

            if (interfaces.Count > 1 || interfaces.Count == 0)
            {
                throw new Exception("MsgController的实现类必须继承Sharing中共享的接口," +
                                    "且不能继承MsgController外的其他接口,且不能多层继承");
            }

            for (int k = 0; k < interfaces.Count; k++)
            {
                this.ControllerObj     = Activator.CreateInstance(t);
                this.packageName       = t.Namespace;
                this.FullName          = t.FullName;
                this.className         = t.Name;
                this.interfaceFullName = interfaces[k].FullName;
                try
                {
                    #region 方法二
                    MethodInfo[] info = t.GetMethods();
                    for (int i = 0; i < info.Length; i++)
                    {
                        MsgFun mf = new MsgFun();

                        MethodInfo md = info[i];
                        if (md.Name.Equals("ToString") || md.Name.Equals("Equals") || md.Name.Equals("GetHashCode") || md.Name.Equals("GetType"))
                        {
                            continue;
                        }
                        //方法名
                        string mothodName = md.Name;
                        Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name}");
                        //参数集合
                        ParameterInfo[] paramInfos = md.GetParameters();

                        MsgFun mfs = new MsgFun();
                        mfs.Name = md.Name;
                        mfs.rep  = md.ReturnType;

                        //var objj = Activator.CreateInstance(mfs.req);
                        mfs.methodInfo = md;
                        this.put(md.Name, mfs);

                        for (int j = 0; j < paramInfos.Length; j++)
                        {
                            ParameterInfo parameterInfo = paramInfos[j];
                            mfs.req = parameterInfo.ParameterType;

                            Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name},{"入参" + j + ":" + parameterInfo.ParameterType}");
                        }
                        FleckLog.Level = LogLevel.Debug;
                        var allSockets = new List <IWebSocketConnection>();
                        var server     = new WebSocketServer($"{sockettype }://{ipaddres}:{ ipport}//{this.interfaceFullName + "." + md.Name}");

                        Action <IWebSocketConnection> config = new Action <IWebSocketConnection>(socket =>
                        {
                            socket.OnOpen = () =>
                            {
                                allSockets.Add(socket);
                            };
                            socket.OnClose = () =>
                            {
                                Console.WriteLine();
                                allSockets.Remove(socket);
                            };
                            socket.OnMessage = message =>
                            {
                                SuperMsg superMsg = this.serializer.DeSerializeString <SuperMsg>(message);
                                object[] objs     = new object[] { superMsg.msg };// new object[] { JsonSerializer.CreateDefault().Deserialize( ea.Body.ToArray().ToString(), parameterInfo.ParameterType) };//new object[] { ProtobufSerializer.DeSerializeBytes(parameterInfo.ParameterType, ea.Body.ToArray()) }
                                object rep        = mfs.methodInfo.Invoke(this.ControllerObj, objs);
                                if (md.ReturnType != typeof(void))
                                {
                                    //message 然后开启查询
                                    allSockets.ToList().ForEach(s => s.Send(this.serializer.SerializeString(new SuperMsg(rep))));
                                }
                            };
                        });
                        WeeklyAction weeklyAction = new WeeklyAction(SendMsg);
                        weeklyAction.Invoke(server, config);
                        Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name},{"返回值" + ":" + md.ReturnType}");
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }