消息队列 长度不能小于 0。 参数名: length
C# code
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(".//Private$//MSMQDemo");
System.Messaging.Message message = new System.Messaging.Message();
message.Body = textBox1.Text.Trim();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
queue.Send(message);
RT 点击发送后报错:
未处理 System.ArgumentOutOfRangeException
Message=长度不能小于 0。
参数名: length
Source=mscorlib
ParamName=length
------解决方案--------------------
用下面这段试验一下。
// Send Message
private void btnSendMessage_Click(object sender, System.EventArgs e)
{
// Open queue
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(".\\Private$\\MSMQDemo");
// Create message
System.Messaging.Message message = new System.Messaging.Message();
message.Body = txtMessage.Text.Trim();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] {typeof(string)});
// Put message into queue
queue.Send(message);
}
// Receive Message
private void btnReceiveMessage_Click(object sender, System.EventArgs e)
{
// Open queue
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(".\\Private$\\MSMQDemo");
// Receive message, 同步的Receive方法阻塞当前执行线程,直到一个message可以得到
System.Messaging.Message message = queue.Receive();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] {typeof(string)});
txtReceiveMessage.Text = message.Body.ToString();
}