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

  • Home
  • SEARCH
  • 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 3236676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:38:58+00:00 2026-05-17T17:38:58+00:00

I have a .NET 4 WCF service that maintains a thread-safe, in-memory, dictionary cache

  • 0

I have a .NET 4 WCF service that maintains a thread-safe, in-memory, dictionary cache of objects (SynchronizedObject). I want to provide safe, concurrent access to read and modify both the collection and the objects in the collection. Safely modifying the objects and the cache can be accomplished with reader-writer locks.

I am running into trouble providing read access to an object in the cache. My Read method returns a SynchronizedObject, but I do not know how to elegantly ensure no other threads are modifying the object while WCF is serializing the SynchronizedObject.

I have tried placing the Read return clause inside the read-lock and setting a breakpoint in a custom XmlObjectSerializer. When the XmlObjectSerializer::WriteObject(Stream,object) method is called, a read-lock is not held on the SynchronizedObject.

I am specifically concerned with the following scenario:

  1. Thread A calls Read(int). Execution continues until just after the return statement. By this point, the finally has also been executed, and the read lock on the SynchronizedObject has been released. Thread A’s execution is interrupted.
  2. Thread B calls Modify(int) for the same id. The write lock is available and obtained. Sometime between obtaining the write lock and releasing it, Thread B is interrupted.
  3. Thread A restarts and serialization continues. Thread B has a write-lock on the same SynchronizedObject, and is in the middle of some critical section, but Thread A is reading the state of the SynchronizedObject and thus returns a potentially invalid object to the caller of Read(int).

I see two options:

  • Maintain a custom XmlObjectSerializer that grabs the read-lock before calling the base.WriteObject(Stream, object) method, and releases it after. I do not like this option because sub-classing and overriding a framework serialization function to perform a certain action if a the object to be serialized matches a certain type smells to me.
  • Create a deep-copy of a SynchronizedObject in the Read method while the read-lock is held, release the lock, and return the deep copy. I do not like this option because there will be many sub-classes of SynchronizedObject that I would have to implement and maintain correct deep-copiers for and deep-copies could be expensive.

What other options do I have? How should I implement the thread-safe Read method?

I have provided a dummy Service below for more explicit references:


    public class Service : IService
    {
        IDictionary<int, SynchronizedObject> collection = new Dictionary<int, SynchronizedObject>();
        ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();

        public SynchronizedObject Read(int id)
        {
            rwLock.EnterReadLock();
            try
            {
                SynchronizedObject result = collection[id];
                result.rwLock.EnterReadLock();
                try
                {
                    return result;
                }
                finally
                {
                    result.rwLock.ExitReadLock();
                }
            }
            finally
            {
                rwLock.ExitReadLock();
            }
        }

        public void ModifyObject(int id)
        {
            rwLock.EnterReadLock();
            try
            {
                SynchronizedObject obj = collection[id];
                obj.rwLock.EnterWriteLock();
                try
                {
                    // modify obj
                }
                finally
                {
                    obj.rwLock.ExitWriteLock();
                }
            }
            finally
            {
                rwLock.ExitReadLock();
            }
        }

        public void ModifyCollection(int id)
        {
            rwLock.EnterWriteLock();
            try
            {
                // modify collection
            }
            finally
            {
                rwLock.ExitWriteLock();
            }
        }
    }

    public class SynchronizedObject
    {
        public ReaderWriterLockSlim rwLock { get; private set; }

        public SynchronizedObject()
        {
            rwLock = new ReaderWriterLockSlim();
        }
    }
  • 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-17T17:38:58+00:00Added an answer on May 17, 2026 at 5:38 pm

    New answer

    Based on your new information and clearer scenario, I believe you want to use something similar to functional programming’s immutability feature. Instead of serializing the object that could be changed, make a copy that no other thread could possibly access, then serialize that.

    Previous (not valuable) answer

    From http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.enterwritelock.aspx:

    If other threads have entered the lock
    in read mode, a thread that calls the
    EnterWriteLock method blocks until
    those threads have exited read mode.
    When there are threads waiting to
    enter write mode, additional threads
    that try to enter read mode or
    upgradeable mode block until all the
    threads waiting to enter write mode
    have either timed out or entered write
    mode and then exited from it.

    So, all you need to do is call EnterWriteLock and ExitWriteLock inside ModifyObject(). Your attempt to make sure you have both a read and a write lock is actually stopping the code from working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a VB.NET WCF service that recives and sends back data. When
I have a WCF service that I have to reference from a .net 2.0
I have created a reference to an IIS hosted WCF service in my ASP.NET
I have a Silverlight application that communications with an ASP.NET backend through WCF. I
If I have .Net Form with a component/object such as a textbox that I
I have a .NET webforms front end that allows admin users to upload two
Does .net have a way to determine whether the local filesystem is case-sensitive?
I have a .Net desktop application with a TreeView as one of the UI
I have a .NET 2.0 windows forms app, which makes heavy use of the
I have a .NET application, which is using an open source C++ compression library

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.