/// <summary> /// 异步请求处理 /// </summary> /// <param name="message"></param> /// <returns></returns> public async Task <ResponseMessage> RequestAsync(RequestMessage message) { try { if (string.IsNullOrWhiteSpace(message.Id)) { return(ResponseMessage.GetResponseMessageFromErrorCode(message, ErrorCodes.MessageIdNullOrWhiteSpace)); } //获取服务对应的消息处理器服务 if (DuplexServicePool.TryGetMessageHandler(ServiceHost, out var messageHandler)) { //获取对应此消息Id的请求处理 if (messageHandler.TryGetMessageListener(message.Id, out Func <RequestMessage, Task <ResponseMessage> > requestAsyncFunc)) { return(await requestAsyncFunc.Invoke(message)); } return(ResponseMessage.GetResponseMessageFromErrorCode(message, ErrorCodes.GetRequestMessageListenerFailed)); } return(ResponseMessage.GetResponseMessageFromErrorCode(message, ErrorCodes.GetMessageHandlerFailed)); } catch (Exception e) { return(ResponseMessage.ExceptionResponseMessage(message, e, false)); } }
/// <summary> /// 通知处理 /// </summary> /// <param name="message"></param> public void Notify(NotifyMessage message) { try { //获取服务对应的消息处理器服务 if (DuplexServicePool.TryGetMessageHandler(ServiceHost, out var messageHandler)) { //获取对应此消息Id的通知处理 if (messageHandler.TryGetMessageListener(message.Id, out Action <NotifyMessage> notifyAction)) { notifyAction.Invoke(message); } } } catch (Exception) { //服务端此处必须忽略异常,否则会导致通信断开 } }