简体   繁体   English

如何为MSMQ创建C#侦听器服务作为Windows服务

[英]How to Create a C# Listener Service for MSMQ as a Windows Service

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. 我将首先说我不是.NET开发人员,但是我已经被投入到需要使用MSMQ的项目中,因此经典的ASP Web应用程序可以将消息发送到处理处理的C#Windows服务。 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. 我有将其他消息队列与其他语言集成的经验,但正如我所提到的,我对.NET和Windows开发没有太多经验,因此我们非常感谢一些指导。

Here are my questions... 这是我的问题......

  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? 有人可以提供一些基本的C#代码来监听现有的MSMQ队列并通过执行一些简单的操作来响应新消息,例如将当前时间戳写入日志文件或发送电子邮件吗?

  2. How do I package this code up in Visual Studio .NET to create and install a Windows Service? 如何在Visual Studio .NET中打包此代码以创建和安装Windows服务? (What type of project should it be, etc. I'm using Visual C# 2010 Express.) (它应该是什么类型的项目,等等。我正在使用Visual C#2010 Express。)

  3. Finally, I'm not sure which version and/or implementation of MSMQ I need to be using for my requirements with classic ASP. 最后,我不确定我需要使用哪种版本和/或MSMQ实现来满足我对经典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. 我认为COM版本是我需要的,但我也读过一个新的WCF版本,以及3.0和4.0之间的差异。 Could someone please give me direction on which version I should be using? 有人可以指点我应该使用哪个版本?

Many thanks! 非常感谢!

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") 您可以使用以下代码等待给定队列上的消息(您希望在计算机上使用名为SomeQueue的专用队列,名为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();
        }
    }

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. 注意:Receove方法将阻止直到收到一条消息,此时它将从队列中删除该消息并将其返回。

edit: added code for the implementation of the multithreaded portion (and renamed the above method signature) 编辑:为多线程部分的实现添加了代码(并重命名了上面的方法签名)

Thread Creation 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 我要注意,这是未经测试的鳕鱼,它只是一个简单的例子

As far as I know, Visual Studio Express does not have a project template for a service. 据我所知,Visual Studio Express没有服务的项目模板。 That does not mean you cannot write a windows service with VSE, just that you will not have a template to get you started. 这并不意味着您无法使用VSE编写Windows服务,只是因为您没有模板可以帮助您入门。

To create a service you can just create a normal Console application. 要创建服务,您只需创建一个普通的控制台应用程序。 Create the service class which will be responsible for the actual service implementation. 创建将负责实际服务实现的服务类。 It will look something like this 它看起来像这样

using System.ServiceProcess;

namespace WindowsService1
{
  public partial class Service1 : ServiceBase
  {
    public Service1()
    {
      InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
    }

    protected override void OnStop()
    {
    }
  }
}

Then the Service startup code can go into the Main function of your service. 然后,服务启动代码可以进入服务的Main功能。

using System.ServiceProcess;

namespace WindowsService1
{
  static class Program
  {
    static void Main()
    {
      ServiceBase[] ServicesToRun;
      ServicesToRun = new ServiceBase[] 
            { 
                new Service1() 
            };
      ServiceBase.Run(ServicesToRun);
    }
  }
}

That should give you the basic framework for your service. 这应该为您提供服务的基本框架。 The other thing you will need is to add an installer for the service so that it can be installed as a service. 您需要的另一件事是为服务添加安装程序,以便可以将其安装为服务。 The following should get you started, note I have note tested this. 以下内容应该让您入门,注意我已经对此进行了测试。

using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService1
{
  [RunInstaller(true)]
  public class ProjectInstaller : Installer
  {
    private ServiceProcessInstaller serviceProcessInstaller1;
    private ServiceInstaller serviceInstaller1;

    public ProjectInstaller()
    {
      this.serviceProcessInstaller1 = new ServiceProcessInstaller();
      this.serviceInstaller1 = new ServiceInstaller();

      this.serviceProcessInstaller1.Password = null;
      this.serviceProcessInstaller1.Username = null;

      this.serviceInstaller1.ServiceName = "Service1";

      this.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.serviceProcessInstaller1,
        this.serviceInstaller1});
    }
  }
}

Given the above, you should have enough to search around or ask for more details around the service creation. 鉴于上述情况,您应该有足够的搜索周围或询问有关服务创建的更多详细信息。 As for the MSMQ listener, you can use the following MSDN article as a starting point 对于MSMQ侦听器,您可以使用以下MSDN文章作为起点

http://msdn.microsoft.com/en-us/library/ms978425.aspx http://msdn.microsoft.com/en-us/library/ms978425.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM