/// <summary> /// 发送消息。必传Message。 /// </summary> public static void Send <T>(MsmqParam <T> param) where T : class { var path = param.QueuePath + param.QueueName; CreateMessageQueue(path); var envelope = new Message(param.Message) { Recoverable = false }; NewMessageQueue <T>(path).Send(envelope); }
/// <summary> /// 接收消息 /// </summary> public static T Receive <T>(MsmqParam <T> param) where T : class { var path = param.QueuePath + param.QueueName; CreateMessageQueue(path); var receiveQueue = NewMessageQueue <T>(path); var message = receiveQueue.Receive(); if (message != null) { return(message.Body as T); } return(null); }
/// <summary> /// 发送消息并异步接收一个消息。必传Message。 /// </summary> public static void SendAndReceiveAsyn <T>(MsmqParam <T> param, Action <T> callBack, bool loop = false) where T : class { var path = param.QueuePath + param.QueueName; CreateMessageQueue(path); var envelope = new Message(param.Message) { Recoverable = false }; NewMessageQueue <T>(path).Send(envelope); param.CallBack = callBack; ReceiveAsyn(param, loop); }
/// <summary> /// 异步接收消息。必传CallBack /// </summary> public static void ReceiveAsyn <T>(MsmqParam <T> param, bool loop = false) where T : class { var path = param.QueuePath + param.QueueName; CreateMessageQueue(path); var receiveQueue = NewMessageQueue <T>(path); receiveQueue.ReceiveCompleted += (sender, args) => { var result = args.Message.Body as T; param.CallBack(result); if (loop) { receiveQueue.BeginReceive(); } }; receiveQueue.BeginReceive(); }
/// <summary> /// 传入各种参数。 /// </summary> public MsmqHelper(MsmqParam param) { Init(param); }