SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to execute a piece of code on application start. As application_start not available in share point, so i have written my own custom HTTPModule and in Init method i have written the code. But the problem here is, the Init method is calling multiple times and the same code executing several times. I want to execute the code only once.

I have used lock mechanism code suggested from After writing the lock mechanism code in HTTPModule, when i browse the site pages which has web parts then i am getting NULLRefernece Exception. If i remove the lock mechanism code, its working fine.

can you please suggest me how to execute the Init code only once.


share|improve this question
So you already know that a HttpModule is instantiated via its Init Method multiple times per request. This is normal behavior. The trick to get your code to execute once is by interrogating the Request Object and filtering out all other requests apart from the one made by the user. I cannot remember exactly which property you should interrogate and that's why i'm not posting the answer, but this should get you half way there. I'll come back once I've found the answer. – Fox Jan 30 '13 at 8:06
I am getting current request as "Request '(System.Web.HttpContext.Current.ApplicationInstance).Request' threw an exception of type 'System.Web.HttpException'" and it saying as "request is not avalible in this context". so how can i know that request comes from user. – Jan 30 '13 at 9:08
So have to checked if any of the Threads is not null? That would be the one that you need to use... So in other words, do a check to see if HttpContext.Current.Request is Not Null. Also, as far as I can remember you have to cast the current Handler to get the Request object. – Fox Jan 30 '13 at 9:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.