- 爱易网页
-
ASP.NET教程
- 关于MSMQ消息队列的有关问题
日期:2014-05-17 浏览次数:20412 次
关于MSMQ消息队列的问题!
刚学有关消息队列的编程,已经手动创建了一个消息队列.\\Private$\\psorders,并标注为事务性
我不理解MessageQueueTransactionType transactionType = MessageQueueTransactionType.Single和automatic的具体用法,我个人认为既然消息队列为事务性的话,应该支持事务回滚,既send成功时,如果receive失败消息应该不会被删掉,可是查了消息队列的内容还是被删了,这跟当初没有标注为事务性的结果一样,望高手给小弟指明(ps:receive()里的注释只是为了验证失败的情况,去注释后可正常运行)
using System;
using System.Messaging;
namespace PetShop.MSMQMessaging
{
class Program
{
public static void send()
{
MessageQueue queue = new MessageQueue(".\\Private$\\psorders");
MessageQueueTransactionType transactionType = MessageQueueTransactionType.Single;
Message message = new Message();
message.Body = "yourd";
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] {typeof(string)});
queue.Formatter = new BinaryMessageFormatter();
queue.Send(message, transactionType);
Console.WriteLine("one");
}
public static void receive()
{
MessageQueue queue1 = new MessageQueue(".\\Private$\\psorders");
MessageQueueTransactionType transactionType = MessageQueueTransactionType.Single;
queue1.Formatter = new BinaryMessageFormatter();
TimeSpan timeout = TimeSpan.FromSeconds(Convert.ToDouble(5));
System.Messaging.MessageQueueTransaction transaction = new System.Messaging.MessageQueueTransaction();
transaction.Begin();
try{
Message message = queue1.Receive(timeout, transactionType);
// message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
string text = message.Body.ToString();
Console.WriteLine(text);
transaction.Commit();
}
catch
{