One of my recent projects involved sending reminder e-mails based on some reminder configuration. E-mail sending is implemented in an asynchronous job using Quartz.net. and it requires to include a permalink to a entity in the application.
However, in order to get this permalink, I have to be able to compute the full URL, based on the identifier stored in the queue used for the job. The answer is not straightforward, since HttpContext
is not available in the thread's context.
One solution is to store application root path in the queue and use it from there. Another way is to use a function like the following:
public static String GetCurrentBasePath(String prefix = "http://")
{
return String.Format("{0}{1}{2}",
prefix, // protocol
System.Net.Dns.GetHostEntry("").HostName, // host
System.Web.HttpRuntime.AppDomainAppVirtualPath // virtual application path
);
}
However, this has (severe) limitations, since the protocol must be provided and also returns the hostname
, not the domain name
, thus rendering it useless when having multiple Web applications bound to the same host.
The question: is Web application base path available in another thread/task?. I think the other thread/task context is somehow connected to the ApplicationPool
, rather than the WebApp
and since multiple WebApp
s can use the same ApplicationPool
, there is not a direct connection between thread context and the WebApp
.