Question

In Application_Start event in global.asax.cs, I have added some initialization code. This code is run in a spawned thread (created using new Thread()). The initialization code uses Server.MapPath to get the physical path of a file. However many times I get an error in the log files : Server Operation is not available in this context In any case I am catching the exception. The initialization is not critical since even if it does not succeed, it will not be a problem for me. My question is:

  1. Why am I getting the Server not available error?
  2. Can the handled exception cause any problems like Invalid Viewstate during normal execution of the application. We noticed a lot of these - Invalid Viewstate later in the logs. I believe it cannot. Please correct me if I am wrong.

Thanks

Vikas

Was it helpful?

Solution

the Server object is referenced via HttpContext.Current i.e. relative to the current request. I suspect its not available from Application_Start since there is no request present at that point. Regardless it won't reliably be available from within separate thread because the threads run independently from each other, therefore any request may have started or finished before your thread tries to access it.

Try using HostingEnvironment.MapPath() instead as that is a static method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top