LA.NET [EN]

Jun 11

In my last post on this series, we”ve seen how we can use the RouteTable class to register all the routes available on a web application. I”d just like to add one more thing on the locking topic. As you might recall, we”ve seen that internally the RouteCollection uses locks to guarantee that everything works well in multithreaded scenarios.

At that previous post, I”ve written that:

Besides these methods, the class introduces some utility methods that will let you control the lock in the collection. I’m talking about the GetReadLock and GetWriterLock methods. These methods are used internally to garantee that the route collection plays well in  muthithreaded scenarios. Since these methods are public, this means that you should call them if you decide to go through all the routes stored on a collection by using a foreach loop. Notice that you don’t need to do that if you use the methods presented in the previous list since those methods already take the appropriate locks.

Well, this is true, but not in all the cases. If you look at the RouteCollection”s insertion/deletion methods (Add, InsertItem and RemoveItem), you”ll see that none of them uses a lock. Why? Well, because normally you”ll add your routes during the application start event (as I”ve said in my previous post) and in these scenarios you don”t need to lock anything because you know that at that time there”s only  a single thread. On the other hand, if you need to add/remove routes after that event,then you”ll need to get a lock by calling the GetWriterLock method (btw,don”t forget to dispose it when you don”t need it!).

Special thanks go to Eilon for letting me know this.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>