简体   繁体   English

.NET应用程序的按钮单击需要告诉外部服务从队列(msmq)开始处理

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

I have an asp.net webforms application that users will be interacting with. 我有一个与用户进行交互的asp.net Web窗体应用程序。 When the user selects certain items from the site and clicks a button for processing. 当用户从站点中选择某些项目并单击按钮进行处理时。 The webapp will add a message to msmq for each item the user selects. 对于用户选择的每个项目,Web应用程序都会向msmq添加一条消息。

The part I am trying to figure out: 我要弄清楚的部分:

What type of application should my second application be if its job will be to process the queue (msmq). 如果我的第二个应用程序的工作是处理队列(msmq),则它将是哪种类型的应用程序。 The key is that this second application needs to sit idle until my asp.net webapp tells it that the 'Process' button was clicked, then it will go ahead, check the queue and process it. 关键是第二个应用程序需要闲置,直到我的asp.net webapp告诉它单击了“处理”按钮,然后它将继续进行,检查队列并进行处理。 I do not want it to constantly check the queue every minute or so, only on command check the queue. 我不希望它每分钟左右不断检查队列,仅在命令时检查队列。

Windows Service, Web Service, Web API? Windows服务,Web服务,Web API? I currently have a Windows Service that checks the queue every 30 seconds. 我目前有一个Windows服务,该服务每30秒检查一次队列。 How can I make it work on-demand rather than constantly checking? 如何使它按需工作而不是不断检查?

Here it is reworded slightly different: 这里的措词略有不同:

  1. User is in .NET application and selects items then clicks Process. 用户在.NET应用程序中,然后选择项目,然后单击“处理”。

  2. .Net application adds items that need to be processed to queue (msmq). .Net应用程序将需要处理的项目添加到队列(msmq)。

  3. An additional application will be told by the .NET application that the process button was clicked and that it should start processing the queue. .NET应用程序将告知其他应用程序,该进程按钮已单击,并且应该开始处理队列。 Once the queue is processed the additional application should go back to some sort of idle mode. 处理完队列后,其他应用程序应返回某种空闲模式。

The most important thing here is that I do not want it on a timer that checks if there is items in the queue every minute. 这里最重要的是,我不想在每分钟检查队列中是否有项目的计时器上使用它。 This will eat up resources running 24/7. 这将消耗24/7运行的资源。

Maybe I am unfamiliar with certain features of msmq. 也许我不熟悉msmq的某些功能。 Is there a way msmq can tell my service that it recieved messages in the queue, go ahead and process them. msmq是否可以通过某种方式告诉我的服务它已接收到队列中的消息,继续进行处理。 Rather than my .net application talking to the service? 而不是我的.net应用程序正在与服务对话?

You can use your existing Windows Service. 您可以使用现有的Windows服务。 But instead of checking the queue every 30 seconds or so, why not add a listener to the queue that responds only when a message has arrived. 但是,为什么不每30秒左右检查一次队列,为什么不将侦听器添加到仅在消息到达时才响应的队列中。 Please check snippet below. 请检查下面的代码段。

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