Custom ashx handler may be useful in Sharepoint e.g. when we need to return some server data in json format in order to consume it on the client side. In this case we most probably copy ashx to subfolder of 14 or 15/Layouts folder and call it in context of Sharepoint site: http://example.com/_layouts/foo/bar.ashx. If in codebehind of ashx handler we need to reopen site under elevated privileges like shown below:
we may get exception:
Operation is not valid due to the current state of an object
In order to avoid it we need to use slightly different code:
Hope that it will help someone.