ASP.NET - Get website's URL without HttpContext.Current (running in background thread)
Bit of a long shot, but is there a way in ASP.NET to dynamically get the website's URL (http://www.example.com) when there is no HttpContext.Current available.
There is no HttpContext because the code is running in a background thread* (but under the ASP.NET AppDomain). I h开发者_开发技巧ave a background process that sends emails out every evening, and needs to include the Web address, but I don't want to hard code it because of deployments and testing (it changes from http://localhost:12345 to http://testing.example.com and then to http://www.example.com for the live site).
*Please don't advise a Windows Service, I'm aware of these but hosting restrictions prevent me from doing this.
Your app must construct and start the thread. At the point the thread is created, the HttpContext.Current should be available, store the relevant part of the web address that is important and either pass to the constructor of your object that wraps the threading functionality, or if you dont have a custom object, store it in a location where the thread can access it. You will need to enter a critcial section with lock() {} if you use the latter approach.
Judging by your weird hosting restrictions (seriously, just buy a decent server instead, saves you a lot of trouble) you're starting the thread from an ASP.NET page. Just feed the URL in when you're starting the thread and store it in a field.
精彩评论