簡體   English   中英

如何為MSMQ創建C#偵聽器服務作為Windows服務

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

我將首先說我不是.NET開發人員,但是我已經被投入到需要使用MSMQ的項目中,因此經典的ASP Web應用程序可以將消息發送到處理處理的C#Windows服務。 我有將其他消息隊列與其他語言集成的經驗,但正如我所提到的,我對.NET和Windows開發沒有太多經驗,因此我們非常感謝一些指導。

這是我的問題......

  1. 有人可以提供一些基本的C#代碼來監聽現有的MSMQ隊列並通過執行一些簡單的操作來響應新消息,例如將當前時間戳寫入日志文件或發送電子郵件嗎?

  2. 如何在Visual Studio .NET中打包此代碼以創建和安裝Windows服務? (它應該是什么類型的項目,等等。我正在使用Visual C#2010 Express。)

  3. 最后,我不確定我需要使用哪種版本和/或MSMQ實現來滿足我對經典ASP的要求。 我認為COM版本是我需要的,但我也讀過一個新的WCF版本,以及3.0和4.0之間的差異。 有人可以指點我應該使用哪個版本?

非常感謝!

您可以使用以下代碼等待給定隊列上的消息(您希望在計算機上使用名為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();
        }
    }

注意:Receove方法將阻止直到收到一條消息,此時它將從隊列中刪除該消息並將其返回。

編輯:為多線程部分的實現添加了代碼(並重命名了上面的方法簽名)

線程創建代碼:

        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; 
    }

我要注意,這是未經測試的鱈魚,它只是一個簡單的例子

據我所知,Visual Studio Express沒有服務的項目模板。 這並不意味着您無法使用VSE編寫Windows服務,只是因為您沒有模板可以幫助您入門。

要創建服務,您只需創建一個普通的控制台應用程序。 創建將負責實際服務實現的服務類。 它看起來像這樣

using System.ServiceProcess;

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

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

    protected override void OnStop()
    {
    }
  }
}

然后,服務啟動代碼可以進入服務的Main功能。

using System.ServiceProcess;

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

這應該為您提供服務的基本框架。 您需要的另一件事是為服務添加安裝程序,以便可以將其安裝為服務。 以下內容應該讓您入門,注意我已經對此進行了測試。

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});
    }
  }
}

鑒於上述情況,您應該有足夠的搜索周圍或詢問有關服務創建的更多詳細信息。 對於MSMQ偵聽器,您可以使用以下MSDN文章作為起點

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM