Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 http://stackoverflow.com/questions/1140915/httpmodule-init-method-is-called-several-times-why 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.

Thanks.

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. –  dotnetmirror.com 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
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.