简体   繁体   中英

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

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). 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. 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? I currently have a Windows Service that checks the queue every 30 seconds. 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.

  2. .Net application adds items that need to be processed to queue (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. 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.

Maybe I am unfamiliar with certain features of msmq. Is there a way msmq can tell my service that it recieved messages in the queue, go ahead and process them. Rather than my .net application talking to the service?

You can use your existing Windows Service. 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. 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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