我正在为用于与MSMQ服务器进行通信的MSMQ客户端编写代码。我的发送方法将重复发送同一条消息数千次,但发送更长的消息,这是不期望的。短消息不会发生此问题。接收和发送队列都是专用队列。
可能是什么原因造成的?
我已启用MSMQ日记来监视创建了多少重复的邮件。
发送功能:
private void Send(string content, string queuePath, int timeToBeReceived)
{
// check if queue exist
if (sendQueue == null)
{
// remote private queue
if (queuePath.Contains("FormatName"))
{
sendQueue = new MessageQueue(queuePath);
}
else
{
// private queue path
if (MessageQueue.Exists(queuePath))
{
sendQueue = new MessageQueue(queuePath);
}
else
{
sendQueue = MessageQueue.Create(queuePath);
// Set permissions
sendQueue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);
}
}
}
// create message
Message msg = new Message();
msg.Formatter = new XmlMessageFormatter((new Type[] { typeof(string) }));
try
{
// validation
XmlDocument xml = new XmlDocument();
xml.LoadXml(content);
xml = null;
msg.BodyStream = new MemoryStream();
var bytes = System.Text.Encoding.UTF8.GetBytes(content);
msg.BodyStream.Write(bytes, 0, bytes.Length);
if (timeToBeReceived > 0)
{
msg.TimeToBeReceived = TimeSpan.FromMilliseconds(timeToBeReceived);
}
sendQueue.Send(msg);
Array.Clear(bytes, 0, bytes.Length);
msg.Dispose();
}
catch (XmlException ex) {
MessageBox.Show(ex.Message, "Wrong XML Format Detected!", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
}
从MainViewModule调用此函数时,TimeToBeReceived默认为0
private RelayCommand _sendCommand;
public RelayCommand SendCommand
{
get
{
return _sendCommand
?? (_sendCommand = new RelayCommand(
() =>
{
Send(SendText, Properties.Settings.Default.MSMQSendPath, Properties.Settings.Default.TimeToBeReceived);
SendText = string.Empty;
}));
}
}
邮件正文:
<result>
<job bsn="000789364" barcode="LJS@000789364@004@IP@LPSTEST@.@.@20190610103346@1@0@.@C519 IP LHD@2.6286@755055@151xx1x1xx1611x1xxx32112111x1x21xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@*000789364*=" jobtype="C519 EB LHD" overallresult="pass"></job>
<tester name="LHD" lane="EB Line 6" version="KSK"></tester>
<tests performed="2019-01-09 10:31:27" cycletime="187" faultmessage="any useful text">
<test name="JX6T91A000LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A002LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A003LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A102LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A107LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A111LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A112LBB" result="pass" value="module" status="1"></test>
<test name="JX6T91A209LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A303LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A401LGD" result="pass" value="module" status="1"></test>
<test name="JX6T91A641LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A680LBD" result="pass" value="module" status="1"></test>
<test name="JX6T91A681LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A682LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A790LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A792LCC" result="pass" value="module" status="1"></test>
<test name="JX6T91A793LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91A811LAD" result="pass" value="module" status="1"></test>
<test name="JX6T91B111LEC" result="pass" value="module" status="1"></test>
<test name="JX6T91B302LDD" result="pass" value="module" status="1"></test>
<test name="JX6T91B830LCD" result="pass" value="module" status="1"></test>
</tests>
我仍在研究此问题,并找到了更多信息。我的代码发送了一次正确的消息,然后出现重复消息。
对于重复消息,它没有发送者用户和SID信息。源计算机的GUID和名称也不同。
正确的日记消息属性队列信息采用以下模式: DIRECT = OS:PCName \ privates $ \ queuename。
错误消息的队列格式名称为:
DIRECT = TCP:IPADDRESS \ private $ \ queuename
可能是根本原因?问题消息的大小为2102。