/// <summary> /// 创建消息队列连接并打开 /// </summary> /// <param name="factory">Rabbit连接工厂</param> /// <param name="funQueueReader">消息队列读取</param> /// <param name="options">配置</param> /// <returns>消息队列连接</returns> public static IMessageQueueConnection CreateConnectionAndOpen(this IMessageQueueConnectionFactory factory, Func <string, IRabbitMessageQueueReader> funQueueReader = null, Action <RabbitConnectionWrapInfo> options = null) { if (factory == null) { throw new ArgumentNullException("Rabbit连接工厂不能为null"); } var config = new RabbitConnectionWrapInfo(); if (options != null) { options(config); } var conn = factory.CreateAndOpen(config); IRabbitMessageQueueReader ququeReader = null; if (funQueueReader == null) { ququeReader = new RabbitMessageQueueJson(config.MessageQueueJsonFile); } else { ququeReader = funQueueReader(config.MessageQueueJsonFile); } conn.SetMessageQueueReader(ququeReader); return(conn); }
/// <summary> /// 调用 /// </summary> /// <param name="method">方法</param> /// <param name="message">消息</param> /// <returns>返回数据</returns> public object Call(MethodInfo method, object message) { var assemblyName = method.DeclaringType.Assembly.GetName().Name; var configInfo = rabbitConfigReader.Reader(); if (configInfo == null) { throw new Exception("找不到RabbitConfig配置信息"); } RpcClientAssemblyWrapInfo rpcClientAssemblyWrap; var messQueueInfo = GetMessageQueueByAssemblyName(configInfo, assemblyName, out rpcClientAssemblyWrap); if (rpcClientAssemblyWrap == null) { throw new Exception($"找不到程序集[{assemblyName}]的RPC客户端配置信息"); } var byteSeri = GetByteSerialization(rpcClientAssemblyWrap.RpcClientAssembly.DataType); var byteData = byteSeri.Serialize(message); IRpcClient rpcClient = null; if (dicVirtualPathMapRpcClient.ContainsKey(rpcClientAssemblyWrap.RabbitVirtualPath.VirtualPath)) { rpcClient = dicVirtualPathMapRpcClient[rpcClientAssemblyWrap.RabbitVirtualPath.VirtualPath]; } else { lock (syncDicVirtualPathMapRpcClient) { IMessageQueueConnection conn = null; // 如果连接配置信息不全,则找默认的连接对象 if (string.IsNullOrWhiteSpace(rpcClientAssemblyWrap.RabbitVirtualPath.ConnectionString) && string.IsNullOrWhiteSpace(rpcClientAssemblyWrap.RabbitVirtualPath.ConnectionStringAppConfigName)) { conn = SingleConnectionTool.CreateConnection(); } else { conn = connectionFactoy.CreateAndOpen(new ConnectionWrapInfo() { ConnectionString = rpcClientAssemblyWrap.RabbitVirtualPath.ConnectionString, ConnectionStringAppConfigName = rpcClientAssemblyWrap.RabbitVirtualPath.ConnectionStringAppConfigName }); } connections.Add(conn); rpcClient = conn.CreateRpcClient(messQueueInfo); try { dicVirtualPathMapRpcClient.Add(rpcClientAssemblyWrap.RabbitVirtualPath.VirtualPath, rpcClient); } catch (ArgumentException) { } } } var reData = rpcClient.Call(byteData); // 如果返回值是空或者类型是void,则直接返回null if (reData.IsNullOrLength0() || method.ReturnType.IsTypeVoid()) { return(null); } else { Type targetType = null; // 判断返回类型是否Task if (method.ReturnType.IsTypeNotGenericityTask()) { return(null); } else if (method.ReturnType.IsTypeGenericityTask()) { targetType = method.ReturnType.GetProperty("Result").PropertyType; } else { targetType = method.ReturnType; } return(byteSeri.Deserialize(reData, targetType)); } }