I recently had a problem with multiple publications in an ASP.NET MVC application. The situation was mainly, if someone intentionally scored the submit button, they could force the data to be sent several times, despite the validation logic (both on the server side and on the client side), which should have forbidden this. This happened because their messages passed before the Transaction.Commit() method could be launched upon initial request (all this is done in nHibernate)
MVC ActionMethod looked something like this.
public ActionResult Create(ViewModelObject model) { if(ModelState.IsValid) { // ... var member = membershipRepository.GetMember(User.Identity.Name); // do stuff with member // update member } }
Many solutions were suggested, but I found the C # lock instruction and tried it, so I changed my code to look like this ...
public ActionResult Create(ViewModelObject model) { if(ModelState.IsValid) { // ... var member = membershipRepository.GetMember(User.Identity.Name); lock(member) { // do stuff with member // update member } } }
It worked! None of my testers can reproduce the error! We beat him during the day, and no one can find a flaw. But I'm not everything related to this keyword. I looked at him again for clarification ...
The lock keyword places an operator block as a critical section, obtaining a mutual exclusion lock for a given object, executing an instruction, and then releasing the lock
Ok, that makes sense. Here is my question.
It was too easy
This solution seemed simple, understandable, understandable, efficient and clean. It was way too easy. I know better than thinking that the complex has this simple solution. So I wanted to ask more experienced programmers ...
Is something bad happening, what should I know?
source share