I use a System.Timers.Timer in my Asp.Net application and I need to use the HttpServerUtility.MapPath method which seems to be only available via HttpContext.Current.Server.MapPath. The problem is that HttpContext.Current is null when the Timer.Elapsed event fires.
Is there another way to get a reference to a HttpServerUtility object ? I could inject it in my class’ constructor. Is it safe ? How can I be sure it won’t be Garbage Collected at the end of the current request?
Thanks!
It’s possible to use
HostingEnvironment.MapPath()instead ofHttpContext.Current.Server.MapPath()I haven’t tried it yet in a thread or timer event though.
Some (non viable) solutions I considered;
The only method I care about on
HttpServerUtilityisMapPath. So as an alternative I could useAppDomain.CurrentDomain.BaseDirectoryand build my paths from this. But this will fail if your app uses virtual directories (Mine does).Another approach: Add all the paths I need to the the
Globalclass. Resolve these paths inApplication_Start.