1

I am loading up some values from my database to be displayed to my website users. Since these values won't be changing too much, I am loading them up into a static/shared object.

To ensure that multiple users aren't modifying this same object at the same time, I am using Monitor.Enter (and Monitor.Exit) to lock/block around the loading of these values.

Unfortunately, this does not work when being called from an MVC view in .NET as it throws this exception:

Object synchronization method was called from an unsynchronized block of code.

Is there a way to do this, via MVC, without completely changing my code design?

Thiago Ferreira
  • 651
  • 3
  • 19
Jesse Sierks
  • 2,048
  • 2
  • 18
  • 26

1 Answers1

5

You see this error because you, most probably, use Monitor inside of async method. But you should not, can read more about this issue here monitor in async/await

You can simply replace Monitor with SemaphoreSlim or AutoResetEvent

wonea
  • 4,425
  • 17
  • 82
  • 137
Sergii Apostol
  • 301
  • 1
  • 8