gpt4 book ai didi

c# - 消息在进行事务处理时未到达 MSMQ

转载 作者:可可西里 更新时间:2023-11-01 07:49:35 27 4
gpt4 key购买 nike

我在本地机器上创建了一个私有(private) MSMQ。我正在使用以下 C# 代码将消息发送到队列。当我将队列更改为事务性队列时,消息未到达 MSMQ。但是,在 Send 方法中没有抛出异常。我需要进行哪些更改才能使其正常工作?

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() 版本。对此最大的挫折是它不会像您所看到的那样抛出任何异常或错误,但消息永远不会显示。

因此,在您的代码中,更改:

helpRequestQueue.Send(theMessage); 

helpRequestQueue.Send(theMessage, MessageQueueTransactionType.Single); 

编辑:我的回答只是 David 之外的另一种方法。

关于c# - 消息在进行事务处理时未到达 MSMQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9911843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com