簡體   English   中英

.NET應用程序的按鈕單擊需要告訴外部服務從隊列(msmq)開始處理

[英]Button click of .NET application needs to tell an external service to start processing from a queue (msmq)

我有一個與用戶進行交互的asp.net Web窗體應用程序。 當用戶從站點中選擇某些項目並單擊按鈕進行處理時。 對於用戶選擇的每個項目,Web應用程序都會向msmq添加一條消息。

我要弄清楚的部分:

如果我的第二個應用程序的工作是處理隊列(msmq),則它將是哪種類型的應用程序。 關鍵是第二個應用程序需要閑置,直到我的asp.net webapp告訴它單擊了“處理”按鈕,然后它將繼續進行,檢查隊列並進行處理。 我不希望它每分鍾左右不斷檢查隊列,僅在命令時檢查隊列。

Windows服務,Web服務,Web API? 我目前有一個Windows服務,該服務每30秒檢查一次隊列。 如何使它按需工作而不是不斷檢查?

這里的措詞略有不同:

  1. 用戶在.NET應用程序中,然后選擇項目,然后單擊“處理”。

  2. .Net應用程序將需要處理的項目添加到隊列(msmq)。

  3. .NET應用程序將告知其他應用程序,該進程按鈕已單擊,並且應該開始處理隊列。 處理完隊列后,其他應用程序應返回某種空閑模式。

這里最重要的是,我不想在每分鍾檢查隊列中是否有項目的計時器上使用它。 這將消耗24/7運行的資源。

也許我不熟悉msmq的某些功能。 msmq是否可以通過某種方式告訴我的服務它已接收到隊列中的消息,繼續進行處理。 而不是我的.net應用程序正在與服務對話?

您可以使用現有的Windows服務。 但是,為什么不每30秒左右檢查一次隊列,為什么不將偵聽器添加到僅在消息到達時才響應的隊列中。 請檢查下面的代碼段。

static void Main(string[] args)
{
    //create an event
    MessageQueue msmq = new MessageQueue("queuename");
    //subscribe to the event
    msmq.ReceiveCompleted += msmq_ReceiveCompleted;
    //start listening
    msmq.BeginReceive();
}

//this will be fired everytime a message is received
static void msmq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
    //get the MessageQueue that we used 
    MessageQueue msmq = sender as MessageQueue;

    //read the message
    //and process it
    Message msg = msmq.EndReceive(e.AsyncResult);

    //because this method is only fired once everytime
    //a message is sent to the queue
    //we have to start listening again            
    msmq.BeginReceive();
}

暫無
暫無

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

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