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
  • 661
  • 3
  • 19
Jesse Sierks
  • 2,258
  • 3
  • 19
  • 30

1 Answers1

6

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,783
  • 17
  • 86
  • 139
Sergii Apostol
  • 311
  • 1
  • 8