如何为MSMQ创建一个C#侦听器服务作为Windows服务 [英] How to Create a C# Listener Service for MSMQ as a Windows Service

查看:416
本文介绍了如何为MSMQ创建一个C#侦听器服务作为Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说开始我不是一个.NET开发人员,但都被扔进一个项目,我需要使用MSMQ这样一个传统的ASP Web应用程序可以将消息发送给一个C#Windows服务处理的处理。我有经验整合与其他语言的其他消息队列,但就像我所说的,我没有与.NET和Windows开发了丰富的经验使一些指导将是非常美联社preciated。

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.

下面是我的问题...

Here are my questions...


  1. 有人可以提供一些基本的C#code侦听到现有的MSMQ队列,并做一些简单的像写当前时间戳到一个日志文件或发送电子邮件响应新的消息?

  1. 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这个code到创建和安装Windows服务? (它应该是什么类型的项目,等我使用Visual C#2010前preSS)。

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.)

最后,我不知道我需要使用我用传统的ASP要求哪个版本和/或执行MSMQ的。我认为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?

非常感谢!

推荐答案

您可以使用以下code(您想使用您的计算机上名为SomeQueue专用队列,命名为计算机名等待消息在给定队列=> QUEUENAME = @计算机名\\私人$ \\ 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 = @"ComputerName\private$\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.

编辑:添加code的多线程部分的实现(并改名为上述方法签名)

edit: added code for the implementation of the multithreaded portion (and renamed the above method signature)

主题创作code:

        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服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆