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 7736463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:44:16+00:00 2026-06-01T07:44:16+00:00

We have a simple WCF service that is tagged with InstanceContextMode = Single and

  • 0

We have a simple WCF service that is tagged with InstanceContextMode = Single and ConcurrencyMode = Multiple. For methods that return Lists, rather than going out to our database and filling the list when the call is made, we simply return a local copy of the list. The list is maintained by a background thread that goes out to our database every 24 hours and repopulates the list. We handle the concurrency issues by locking on an object in 2 places: inside the method returning the collection, and inside the method filling the collection.

My question is, how can we make this more efficient. Currently, we have a bottleneck in out service method “GetCustomers” that the client will call:

public List<ZGpCustomer> GetCustomers()
{
    List<ZGpCustomer> customerListCopy = new List<ZGpCustomer>();
    lock (_customerLock)
    {
        customerListCopy = new List<ZGpCustomer>(_customers);
    }

    return customerListCopy;
}

Since “_customers” is being filled only every 24 hours, it seems like we should only every need to lock inside the GetCustomers method ONLY when we’re modifying the collection. The way it is currently set up, if 1000 requests come in at the same time, we’re effectively queuing the 1000 requests since only 1 thread will have access to that method at a time. This is rendering the multithreading aspect of the service somewhat useless, no?

Is there a best practice for this sort of pattern? Should I be using a more appropriate data collection to store my objects in? Would a “BlockingCollection” be a more suitable fit?

  • 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-06-01T07:44:18+00:00Added an answer on June 1, 2026 at 7:44 am

    There most certainly is a more efficient way of doing this. The trick is to keep the master reference to the collection immutable. That way you never have to synchronize access on the reader side. Only the writer side of things needs a lock. The reader side needs nothing which means the readers stay highly concurrent. The only thing you need to do is mark the _customers reference as volatile.

    // Variable declaration
    object lockobj = new object();
    volatile List<ZGpCustomer> _customers = new List<ZGpCustomer>();
    
    // Writer
    lock (lockobj)
    {
      // Create a temporary copy.
      var copy = new List<ZGpCustomer>(_customers);
    
      // Modify the copy here.
      copy.Add(whatever);
      copy.Remove(whatever);
    
      // Now swap out the references.
      _customers = copy;
    }
    
    // Reader
    public List<ZGpCustomer> GetCustomers()
    {
        return new List<ZGpCustomer>(_customers);
    }
    

    If you are willing to change the signature of GetCustomers slightly you could exploit the immutability of _customers and return a readonly wrapper.

    // Reader
    public IList<ZGpCustomer> GetCustomers()
    {
        return _customers.AsReadOnly();
    }
    
    • 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 exposes a bunch of methods that return business
I wrote the most simple wcf service that have one method // return a+b
I have written a simple WCF service that accepts and stores messages. It works
i have a simple xml file in a wcf service that i am trying
I have written a very simple WCF service, that worked fine (code below), then
I have a simple WCF service that I call server side from code behind
I have a simple WCF service that is used for cross app-domain communication in
I have a simple WCF service that i'm communicating with Asynchronously. The thing i
I've been trying to do a simple restful wcf service that will return JSON.
I have a simple WCF service that will analyse a raw text extracted from

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.