/// <summary> /// 订阅消息 /// </summary> /// <param name="receiveMessageFun">接收消息回调</param> /// <param name="receiveMessageType">接收消息类型</param> /// <param name="isAutoAck">是否自动应答,如果为否,则需要在回调里返回true</param> public void Subscribe(Func <object, bool> receiveMessageFun, Type receiveMessageType, bool isAutoAck = false) { if (receiveMessageFun == null) { return; } Subscribe((byte[] x) => { object data = null; try { data = BytesSerialization.Deserialize(x, receiveMessageType); } catch (MessagePackSerializationException) { // 如果messagepack反序列失败,则使用json反列化 try { data = jsonSerialization.Deserialize(x, receiveMessageType); } catch (Exception ex) { string logMsg = $"{GetLogTitleMsg()}.输入参数isAutoAck:{isAutoAck},jsonSerialization.Deserialize发生异常(返回应答为true),认为是不符合业务规范的数据,应删除消息"; log.ErrorAsync(logMsg, ex, tags: GetLogTags()); // 反序列异常则返回true return(true); } } catch (Exception ex) { string logMsg = $"{GetLogTitleMsg()}.输入参数isAutoAck:{isAutoAck},BytesSerialization.Deserialize发生异常(返回应答为true),认为是不符合业务规范的数据,应删除消息"; log.ErrorAsync(logMsg, ex, tags: GetLogTags()); // 反序列异常则返回true return(true); } try { return(receiveMessageFun(data)); } catch (Exception ex) { var busEx = AmqpUtil.BuilderBusinessException(ex, data, amqpQueue, log, ex.Message); var isAck = ExceptionHandle.Handle(busEx); string logMsg = $"{GetLogTitleMsg()}.输入参数isAutoAck:{isAutoAck},业务处理发生异常(返回应答为{isAck})"; log.ErrorAsync(logMsg, ex, tags: GetLogTags()); return(isAck); } }, isAutoAck); }
private async Task SocketPipeClient_RequestReceived(object sender, SocketRequestReceivedEventArgs args) { MethodCallInfo methodCallInfo = (MethodCallInfo)BytesSerialization.BytesArrayToObject(args.RawBytes); var invoke_result = await methodCallInfo.InvokeAsync(GetExcuteObject()); var response = new MethodCallInfo() { Name = methodCallInfo.Name, Result = invoke_result, }; var resBytes = BytesSerialization.ObjectToBytesArray(response); var status = await args.SendResponseAsync(resBytes); }
public object Invoke(MethodInfo targetMethod, object[] args) { MethodCallInfo methodCall = new MethodCallInfo() { Name = targetMethod.Name, Args = new List <object>(args), }; byte[] methodCallBytes = BytesSerialization.ObjectToBytesArray(methodCall); object result = SendBytesAsync(methodCallBytes).ContinueWith( async task => { byte[] responseBytes = await task; MethodCallInfo responseMethodCall = (MethodCallInfo)BytesSerialization.BytesArrayToObject(responseBytes); return(responseMethodCall.Result); }).Unwrap(); return(result); }
/// <summary> /// 订阅消息 /// </summary> /// <typeparam name="T">接收类型</typeparam> /// <param name="receiveMessageFun">接收消息回调</param> /// <param name="isAutoAck">是否自动应答,如果为否,则需要在回调里返回true</param> public void Subscribe <T>(Func <T, bool> receiveMessageFun, bool isAutoAck = false) { Subscribe((byte[] x) => { if (receiveMessageFun != null) { T data = default(T); try { data = BytesSerialization.Deserialize <T>(x); } catch (Exception ex) { string logMsg = string.Format("输入参数isAutoAck:{0},BytesSerialization.Deserialize发生异常(返回应答为true),认为是不符合业务规范的数据,应删除消息。队列:{1}", isAutoAck, rabbitMessageQueueInfo.Queue); Log.ErrorAsync(logMsg, ex, tags: GetLogTags(rabbitMessageQueueInfo)); // 反序列异常则返回true return(true); } try { return(receiveMessageFun(data)); } catch (Exception ex) { var isAck = PublishExceptionQueue(ex, data); string logMsg = string.Format("输入参数isAutoAck:{0},业务处理发生异常(返回应答为{1}),队列:{2}", isAutoAck, isAck, rabbitMessageQueueInfo.Queue); Log.ErrorAsync(logMsg, ex, tags: GetLogTags(rabbitMessageQueueInfo)); return(isAck); } } return(true); }, isAutoAck); }
/// <summary> /// 订阅消息 /// </summary> /// <typeparam name="T">接收类型</typeparam> /// <param name="receiveMessageFun">接收消息回调</param> /// <param name="isAutoAck">是否自动应答,如果为否,则需要在回调里返回true</param> public void Subscribe <T>(Func <T, bool> receiveMessageFun, bool isAutoAck = false) { Subscribe((byte[] x) => { if (receiveMessageFun != null) { T data = default(T); try { data = BytesSerialization.Deserialize <T>(x); } catch (Exception ex) { string logMsg = $"{GetLogTitleMsg()}.输入参数isAutoAck:{isAutoAck},BytesSerialization.Deserialize发生异常(返回应答为true),认为是不符合业务规范的数据,应删除消息"; log.ErrorAsync(logMsg, ex, tags: GetLogTags()); // 反序列异常则返回true return(true); } try { return(receiveMessageFun(data)); } catch (Exception ex) { var busEx = AmqpUtil.BuilderBusinessException(ex, data, amqpQueue, log, ex.Message); var isAck = ExceptionHandle.Handle(busEx); string logMsg = $"{GetLogTitleMsg()}.输入参数isAutoAck:{isAutoAck},业务处理发生异常(返回应答为{isAck})"; log.ErrorAsync(logMsg, ex, tags: GetLogTags()); return(isAck); } } return(true); }, isAutoAck); }
/// <summary> /// 监听 /// </summary> public void Listen() { RpcServer.Receive(inData => { object result = null; try { var rpcDataInfo = BytesSerialization.Deserialize <RpcDataInfo>(inData); if (rpcDataInfo == null) { OnReceivingError("传过来的数据不是RpcDataInfo类型的"); } else if (string.IsNullOrWhiteSpace(rpcDataInfo.MethodFullPath)) { OnReceivingError("方法全路径不能为空"); } else { string classFullName; var methodName = ReflectUtil.GetMethodName(rpcDataInfo.MethodFullPath, out classFullName); var implClassFullName = InterfaceMapImpl.Reader(classFullName); MethodInfo method; var methodReturnValue = MethodCall.Invoke(string.Format("{0}.{1}", implClassFullName, methodName), out method, rpcDataInfo.MethodParams); // 如果方法返回是Void,则直接返回null if (method.IsMethodReturnVoid()) { return(null); } // 如果方法是异步方法,则转换为Task并等待执行结束后返回Result给客户端 else if (method.ReturnType.IsTypeTask()) { // 如果带泛型,则返回任务的Result给客户端 if (method.ReturnType.IsTypeGenericityTask()) { var resultProperty = method.ReturnType.GetProperty("Result"); result = resultProperty.GetValue(methodReturnValue); } else { var methodReturnTask = methodReturnValue as Task; methodReturnTask.Wait(); } } else { result = methodReturnValue; } } if (result == null) { return(null); } return(BytesSerialization.Serialize(result)); } catch (Exception ex) { try { OnReceivingError(ex.Message, ex); } catch { } return(null); } }); }