进行事务处理时,消息未到达MSMQ [英] Message does not reach MSMQ when made transactional
问题描述
我在本地计算机上创建了一个私人MSMQ.我正在使用以下C#代码将消息发送到队列.当我将队列更改为事务性队列时,消息未到达MSMQ.但是,在Send方法中不会引发任何异常.我需要进行哪些更改才能使其正常工作?
I have a private MSMQ created in my local machine. I am sending messages to the queue using following C# code. When I changed the queue to be transactional, the message is not reaching the MSMQ. However, there is no exception thrown in the Send method. What change I need to make in order to make it working?
using System;
using System.Messaging;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
//Sharing violation resulted from queue being open already for exclusive receive.
MessageQueue helpRequestQueue = new MessageQueue(@".\Private$\MyPrivateQueue", false);
protected void Page_Load(object sender, EventArgs e)
{
bool isTransactionalQueue = false;
if (!System.Messaging.MessageQueue.Exists(@".\Private$\MyPrivateQueue"))
{
System.Messaging.MessageQueue.Create(@".\Private$\MyPrivateQueue", isTransactionalQueue);
}
SendMessage();
GetAllMessages();
}
private void SendMessage()
{
System.Messaging.Message theMessage = new System.Messaging.Message("TimeNow is "+DateTime.Now.ToString());
theMessage.Label = "Lijo " + DateTime.Now.ToString();
theMessage.Priority = System.Messaging.MessagePriority.Normal;
helpRequestQueue.Send(theMessage);
}
private void GetAllMessages()
{
DataTable messageTable = new DataTable();
messageTable.Columns.Add("Label");
messageTable.Columns.Add("Body");
//Set Message Filters
MessagePropertyFilter filter = new MessagePropertyFilter();
filter.ClearAll();
filter.Body = true;
filter.Label = true;
filter.Priority = true;
helpRequestQueue.MessageReadPropertyFilter = filter;
//Get All Messages
System.Messaging.Message[] messages = helpRequestQueue.GetAllMessages();
System.Messaging.XmlMessageFormatter stringFormatter = new System.Messaging.XmlMessageFormatter(new string[] { "System.String" });
for (int index = 0; index < messages.Length; index++)
{
string test = System.Convert.ToString(messages[index].Priority);
messages[index].Formatter = stringFormatter;
messageTable.Rows.Add(new string[] {messages[index].Label,messages[index].Body.ToString() });
}
Gridview1.DataSource = messageTable;
Gridview1.DataBind();
}
private void ReceiveAndProcess()
{
}
}
推荐答案
对于已创建为跨变的队列,必须使用包含MessageQueueTransactionType参数的Send()版本.对此最大的沮丧是它不会像您所看到的那样引发任何异常或错误,但是消息永远不会出现.
For queue's that you have created as transanctional, you must use the version of Send() that includes the MessageQueueTransactionType parameter. The biggest frustration with this is that it doesn't throw any exceptions or errors as you have seen, but the message just doesn't ever show up.
因此,在您的代码中,更改:
So, in your code, change:
helpRequestQueue.Send(theMessage);
到
helpRequestQueue.Send(theMessage, MessageQueueTransactionType.Single);
除了大卫的答案,我的答案只是另一种方式.
My answer is just another way to do it aside from David's.
这篇关于进行事务处理时,消息未到达MSMQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!