Michael L Perry
Michael L Perry

Reputation: 7435

Can I filter the messages I receive from a message queue (MSMQ) by some property? (a.k.a. topic)

I am creating a Windows Service in C# that processes messages from a queue. I want to give ops the flexibility of partitioning the service in production according to properties of the message. For example, they should be able to say that one instance processes web orders from Customer A, another batch orders from Customer A, a third web or batch orders from Customer B, and so on.

My current solution is to assign separate queues to each customer\source combination. The process that puts orders into the queues has to make the right decision. My Windows Service can be configured to pull messages from one or more queues. It's messy, but it works.

Upvotes: 5

Views: 4032

Answers (2)

Árpád Varga
Árpád Varga

Reputation: 31

Use GetMessageEnumerator2() like this:

MessageEnumerator en = q.GetMessageEnumerator2();

while (en.MoveNext())
{
    if (en.Current.Label == label)
    {
        string body = ((XmlDocument)en.Current.Body).OuterXml;
        en.RemoveCurrent();
        return body;
    }
}

Upvotes: 3

Esteban Araya
Esteban Araya

Reputation: 29664

No, but you can PEEK into the queue and decide if you really want to consume the message.

Upvotes: 5

Related Questions