Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 217395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:39:12+00:00 2026-05-11T18:39:12+00:00

I have a WCF service that posts messages to a private, non-transactional MSMQ queue.

  • 0

I have a WCF service that posts messages to a private, non-transactional MSMQ queue. I have another WCF service (multi-threaded) that processes the MSMQ messages and inserts them in the database.

My issue is with sequencing. I want the messages to be in certain order. For example MSG-A need to go to the database before MSG-B is inserted. So my current solution for that is very crude and expensive from database perspective.

I am reading the message, if its MSG-B and there is no MSG-A in the database, I throw it back on the message queue and I keep doing that till MSG-A is inserted in the database. But this is a very expensive operation as it involves table scan (SELECT stmt).

The messages are always posted to the queue in sequence.

Short of making my WCF Queue Processing service Single threaded (By setting the service behavior attribute InstanceContextMode to Single), can someone suggest a better solution?

Thanks

Dan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:39:12+00:00Added an answer on May 11, 2026 at 6:39 pm

    Instead of immediately pushing messages to the DB after taking them out of the queue, keep a list of pending messages in memory. When you get an A or B, check to see if the matching one is in the list. If so, submit them both (in the right order) to the database, and remove the matching one from the list. Otherwise, just add the new message to that list.

    If checking for a match is too expensive a task to serialize – I assume you are multithreading for a reason – the you could have another thread process the list. The existing multiple threads read, immediately submit most messages to the DB, but put the As and Bs aside in the (threadsafe) list. The background thread scavenges through that list finding matching As and Bs and when it finds them it submits them in the right order (and removes them from the list).

    The bottom line is – since your removing items from the queue with multiple threads, you’re going to have to serialize somewhere, in order to ensure ordering. The trick is to minimize the number of times and length of time you spend locked up in serial code.

    There might also be something you could do at the database level, with triggers or something, to reorder the entries when it detects this situation. I’m afraid I don’t know enough about DB programming to help there.

    UPDATE: Assuming the messages contain some id that lets you associate a message ‘A’ with the correct associated message ‘B’, the following code will make sure A goes in the database before B. Note that it does not make sure they are adjacent records in the database – there could be other messages between A and B. Also, if for some reason you get an A or B without ever receiving the matching message of the other type, this code will leak memory since it hangs onto the unmatched message forever.

    (You could extract those two ‘lock’ed blocks into a single subroutine, but I’m leaving it like this for clarity with respect to A and B.)

    static private object dictionaryLock = new object();
    static private Dictionary<int, MyMessage> receivedA = 
        new Dictionary<int, MyMessage>();
    static private Dictionary<int, MyMessage> receivedB = 
        new Dictionary<int, MyMessage>();
    
    public void MessageHandler(MyMessage message)
    {
        MyMessage matchingMessage = null;
        if (IsA(message))
        {
            InsertIntoDB(message);
            lock (dictionaryLock)
            {
                if (receivedB.TryGetValue(message.id, out matchingMessage))
                {
                    receivedB.Remove(message.id);
                }
                else
                {
                    receivedA.Add(message.id, message);
                }
            }
            if (matchingMessage != null)
            {
                InsertIntoDB(matchingMessage);
            }
        }
        else if (IsB(message))
        {
            lock (dictionaryLock)
            {
                if (receivedA.TryGetValue(message.id, out matchingMessage))
                {
                    receivedA.Remove(message.id);
                }
                else
                {
                    receivedB.Add(message.id, message);
                }
            }
            if (matchingMessage != null)
            {
                InsertIntoDB(message);
            }
        }
        else
        {
            // not A or B, do whatever
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF service that talks to MSMQ. When I run the service
I have a WCF service that I have to reference from a .net 2.0
I have a WCF Service that should not enter the faulted state. If there's
I have a WCF service that is hosted in a windows application. The service
I have a WCF Service that exposes a method GetCustomers(). The internals of the
I have a WCF service that I call from a windows service. The WCF
I have a WCF service that uses basicHttpbinding in development. Now in product we
I have a WCF service that uses a System.ServiceModel.Syndication.SyndicationFeed to create an RSS feed.
I have a WCF service that is using a custom ServiceAuthorizationManager . The custom
I have a WCF service that uses X.509 certificates for authentication. What's the best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.