Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get host name without using HttpRequest

I want to run a "background job" in my ASP.NET application (periodically, as separate thread). And I need host name (DNS name or IP) to do my tasks. The problem is that the HttpContext.Current may be not available here (it's NULL).

Is there any way to get a host name in not using HttpContext.Current.Request.Url.Host.

like image 964
Tadas Šukys Avatar asked Jan 29 '10 18:01

Tadas Šukys


1 Answers

When the host name is available in HttpContext.Request.Url.Host, it is a result of the host name being part of the request sent by the client. As an example, take a request to this page:

GET /questions/2164261/get-host-name-without-using-httprequest HTTP/1.1
Host: stackoverflow.com
...

When running in a background thread, no request context is available, and there really is no concept of a host name at all. Your only alternative is to store the hostname within the code or in configuration.

Slightly off topic: Running scheduled tasks within a web application is asking for trouble, and spawning threads only deals with a few of them. If at all possible, consider running your scheduled jobs from a Windows service, possibly built using a framework like NCron.

like image 188
Jørn Schou-Rode Avatar answered Oct 01 '22 06:10

Jørn Schou-Rode