web.config issue when multiple user reads the key at the same time

Viewed 42

I am getting this error when multiple user reads the key at the same time , an error occurred loading a configuration file:

The process cannot access file file 'web.config' becuase it is being used by another process

I am using like below in code.

var value = ConfigurationManager.GetSection("keyname") as NameValueCollection;
1 Answers

If you have concurrent read from web.config I suggest you to use ReadWriterLock Class.

In your Class use:

  static ReaderWriterLock rwl = new ReaderWriterLock();
  ....
  rwl.AcquireReaderLock();
  try {
        // It is safe for this thread to read from the shared resource.
        var value = ConfigurationManager.GetSection("keyname") as NameValueCollection;
     }
     finally {
        // Ensure that the lock is released.
        rwl.ReleaseReaderLock();
     }

Documentation https://docs.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock?view=net-5.0

Related