检测 MSMQ 错误
Detecting MSMQ errors
我有以下代码(逐字),我希望它会产生错误:
using System.Messaging;
namespace MsmqTest
{
class Program
{
static void Main()
{
string invalidQueue = @"FormatName:DIRECT=OS:sometrahsname\private$\anothertrahsname";
Enqueue("test",invalidQueue);
}
private static void Enqueue(object o, string queueName)
{
using (MessageQueue msmq = new MessageQueue(queueName))
using (MessageQueueTransaction transaction = new MessageQueueTransaction())
{
msmq.DefaultPropertiesToSend.Recoverable = true;
transaction.Begin();
msmq.Send(new Message(o), transaction);
transaction.Commit();
}
}
}
}
此处我们正在向不存在的服务器上的队列发送消息。我希望收到出错的指示。我没有得到任何。在这种情况下如何检查错误?
注意:为了运行上面的代码,您需要在您的机器上安装 MSMQ。
我之前在我的一个应用程序中偶然发现了这个问题。 MSDN 记录在 MessageQueue.Send 中,消息可能会被发送到死信队列而不会引发异常。这就是你现在正在经历的。我所做的是检查队列是否存在。
using System;
using System.Messaging;
namespace MsmqTest
{
class Program
{
static void Main(string[] args)
{
string invalidQueue = @"FormatName:DIRECT=OS:sometrahsname\private$\anothertrahsname";
Enqueue("test", invalidQueue);
}
private static void Enqueue(object o, string queueName)
{
try
{
MessageQueue msmq = null;
//check if queueName exists
//it also validates if you have access to MSMQ server
if (!MessageQueue.Exists(queueName))
{
msmq = MessageQueue.Create(queueName);
//you can also set the permission here
//because the other application that may be reading
//has different account with the application that created the queue
//set to Everyone for demonstration purposes
msmq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
}
else
{
msmq = new MessageQueue(queueName);
}
using (msmq)
{
using (MessageQueueTransaction transaction = new MessageQueueTransaction())
{
msmq.DefaultPropertiesToSend.Recoverable = true;
transaction.Begin();
msmq.Send(new Message(o), transaction);
transaction.Commit();
}
}
}catch(Exception)
{
//handle error here
}
}
}
}
我有以下代码(逐字),我希望它会产生错误:
using System.Messaging;
namespace MsmqTest
{
class Program
{
static void Main()
{
string invalidQueue = @"FormatName:DIRECT=OS:sometrahsname\private$\anothertrahsname";
Enqueue("test",invalidQueue);
}
private static void Enqueue(object o, string queueName)
{
using (MessageQueue msmq = new MessageQueue(queueName))
using (MessageQueueTransaction transaction = new MessageQueueTransaction())
{
msmq.DefaultPropertiesToSend.Recoverable = true;
transaction.Begin();
msmq.Send(new Message(o), transaction);
transaction.Commit();
}
}
}
}
此处我们正在向不存在的服务器上的队列发送消息。我希望收到出错的指示。我没有得到任何。在这种情况下如何检查错误?
注意:为了运行上面的代码,您需要在您的机器上安装 MSMQ。
我之前在我的一个应用程序中偶然发现了这个问题。 MSDN 记录在 MessageQueue.Send 中,消息可能会被发送到死信队列而不会引发异常。这就是你现在正在经历的。我所做的是检查队列是否存在。
using System;
using System.Messaging;
namespace MsmqTest
{
class Program
{
static void Main(string[] args)
{
string invalidQueue = @"FormatName:DIRECT=OS:sometrahsname\private$\anothertrahsname";
Enqueue("test", invalidQueue);
}
private static void Enqueue(object o, string queueName)
{
try
{
MessageQueue msmq = null;
//check if queueName exists
//it also validates if you have access to MSMQ server
if (!MessageQueue.Exists(queueName))
{
msmq = MessageQueue.Create(queueName);
//you can also set the permission here
//because the other application that may be reading
//has different account with the application that created the queue
//set to Everyone for demonstration purposes
msmq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
}
else
{
msmq = new MessageQueue(queueName);
}
using (msmq)
{
using (MessageQueueTransaction transaction = new MessageQueueTransaction())
{
msmq.DefaultPropertiesToSend.Recoverable = true;
transaction.Begin();
msmq.Send(new Message(o), transaction);
transaction.Commit();
}
}
}catch(Exception)
{
//handle error here
}
}
}
}