问题描述
我首先要说我不是 .NET 开发人员,但我被投入到一个项目中,我需要使用 MSMQ,以便经典的 ASP Web 应用程序可以将消息发送到处理处理的 C# Windows 服务.我有将其他消息队列与其他语言集成的经验,但就像我提到的,我在 .NET 和 Windows 开发方面没有太多经验,因此非常感谢一些指导.
I'll start by saying I'm not a .NET developer, but have been thrown into a project where I need to use MSMQ so a classic ASP web application can send messages to a C# Windows Service that handles the processing. I have experience integrating other message queues with other languages, but like I mentioned, I don't have much experience with .NET and Windows development so some guidance would be much appreciated.
这是我的问题...
有人可以提供一些基本的 C# 代码来侦听现有的 MSMQ 队列并通过执行一些简单的操作(例如将当前时间戳写入日志文件或发送电子邮件)来响应新消息?
Could someone provide some basic C# code that listens to an existing MSMQ queue and responds to the new message by doing something simple like writing the current timestamp to a log file or sending an email?
如何在 Visual Studio .NET 中打包此代码以创建和安装 Windows 服务?(应该是什么类型的项目等等,我用的是Visual C# 2010 Express.)
How do I package this code up in Visual Studio .NET to create and install a Windows Service? (What type of project should it be, etc. I'm using Visual C# 2010 Express.)
最后,我不确定我需要使用哪个版本和/或实现的 MSMQ 来满足我对经典 ASP 的要求.我认为 COM 版本是我需要的,但我也阅读了新的 WCF 版本,以及 3.0 和 4.0 之间的差异.有人可以指导我应该使用哪个版本吗?
Finally, I'm not sure which version and/or implementation of MSMQ I need to be using for my requirements with classic ASP. I think the COM version is what I need, but I've also read about a new WCF version, as well as differences between 3.0 and 4.0. Could someone please give me direction on which version I should be using?
非常感谢!
推荐答案
您可以使用以下代码在给定队列上等待消息(您想在您的计算机上使用名为 SomeQueue 的私有队列,命名为 ComputerName => QueueName= @"计算机名private$SomeQueue")
You can wait for a message on a given queue using the following code (You want to use the private queue named SomeQueue on your computer, named ComputerName => QueueName = @"ComputerNameprivate$SomeQueue")
public void AsyncWatchQueue(object encapsulatedQueueName)
{
Message newMessage;
MessageQueue queue;
string queueName = encapsulatedQueueName as string;
if (queueName == null)
return;
try
{
if (!MessageQueue.Exists(queueName))
MessageQueue.Create(queueName);
else
{
queue = new MessageQueue(queueName);
if (queue.CanRead)
newMessage = queue.Receive();
}
HandleNewMessage(newMessage); // Do something with the message
}
// This exception is raised when the Abort method
// (in the thread's instance) is called
catch (ThreadAbortException e)
{
//Do thread shutdown
}
finally
{
queue.Dispose();
}
}
注意:Receove 方法将一直阻塞,直到收到一条消息,此时它将从队列中删除该消息并返回.
Note: the Receove method will block untill a message is received at which point it'll remove the message from the queue and return it.
为多线程部分的实现添加了代码(并重命名了上述方法签名)
edit: added code for the implementation of the multithreaded portion (and renamed the above method signature)
线程创建代码:
public Thread AddWatchingThread(string QueueName)
{
Thread Watcher =
new Thread(new ParameterizedThreadStart(AsyncWatchQueue));
Watcher.Start(QueueName);
// The thread instance is used to manipulate (or shutdown the thread)
return Watcher;
}
请注意,这是未经测试的鳕鱼,只是一个简单的示例
I'll just note, that this is untested cod, it's just an quick example
这篇关于如何为 MSMQ 创建 C# 侦听器服务作为 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!