Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the HttpServerUtility.MapPath method in a Thread or Timer?

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!

like image 208
Costo Avatar asked Sep 21 '08 20:09

Costo


People also ask

How does server MapPath work?

If path is null , the MapPath method returns the full physical path of the directory that contains the current request for the path. The relative path does not need to specify an existing file or folder for this method to return a value. However, you cannot specify a path outside of the Web application.

What value is return by server MapPath method?

If Path starts with either a forward (/) or backward slash (\), the MapPath method returns a path as if Path were a full, virtual path. If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the . asp file being processed.

What is Server MapPath in MVC?

For example, Server. MapPath() allows a path of "files/file1. doc". It uses the current context to determine the path of the current page, for example, and then creates the relative path from there.

What are the methods and properties of httpserverutility class?

The methods and properties of the HttpServerUtility class are exposed through the intrinsic Server object provided by ASP.NET. Gets the server's computer name. Gets or sets the request time-out value in seconds. Clears the previous exception. Creates a server instance of a COM object identified by the object's programmatic identifier (ProgID).

What does mappath return if path is null?

If path is null, the MapPath method returns the full physical path of the directory that contains the current request for the path. The relative path does not need to specify an existing file or folder for this method to return a value. However, you cannot specify a path outside of the Web application.

What is the difference between relative path and mappath?

The relative path does not need to specify an existing file or folder for this method to return a value. However, you cannot specify a path outside of the Web application. The MapPath method potentially contains sensitive information about the hosting environment. The return value should not be displayed to users.

What is the httpcontext path?

The physical file path on the Web server that corresponds to path. The current HttpContext is null. path is a physical path, but a virtual path was expected. The following example shows how to retrieve the physical file of a relative virtual path. The code resides in the code-behind file for a web page and utilizes the default Server object.


1 Answers

It's possible to use HostingEnvironment.MapPath() instead of HttpContext.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 HttpServerUtility is MapPath. So as an alternative I could use AppDomain.CurrentDomain.BaseDirectory and 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 Global class. Resolve these paths in Application_Start.

like image 143
Costo Avatar answered Oct 09 '22 21:10

Costo