views:

72

answers:

2

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 have 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.

+2  A: 

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.

Matti Virkkunen
Any reason for the downvote?
Matti Virkkunen
"Buy a new server"? That's not helpful at all, and doesn't solve the problem, just avoids it.
mwalker
@mwalker: I know it doesn't solve the problem, however this is something that really should be done via a service. Also, in case you didn't notice, I did also provide a solution...
Matti Virkkunen
@Matti Downvote maybe unfair but it's a legitimate concern, shared-hosting is quite common and not everyone can afford £50 - £100 month for virtual/dedicated hosting. Even in proper businesses with dedicated hardware/servers, there still may be awkward deployment quirks and/or restrictions in place, especially if hosted in external client's servers.
Sunday Ironfoot
@Matti Indeed, in my rage I didn't notice the actual solution. Downvote retracted.
mwalker
I will +1 as well for goodwill. In the future you might put the non-answer advice at the end of the answer instead of the beginning, so the important bits dont get missed by lazy eyes.
David
@David: Actually my "non-answer" was interlaced with the actual answer about 1/4 way through. It wasn't at the very beginning!
Matti Virkkunen
+2  A: 

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.

David
Thanks! I thought of doing this, I'm just worried as there can potentially be different URLs to access the website. For instance, someone could type localhost on the server to access the site, and it'll permanently store the address as localhost, including in the emails sent out. Sorry, I know it sounds a bit obscure, I just worry about this stuff, maybe I'm over-engineering this too much.
Sunday Ironfoot
I understand the concern. If localhost, then its an interactive user on the server itself that started the web app. You could possibly put the thread start code in Application_BeginRequest, and only start the thread when the current requested address is not localhost and possibly even not an internal address (ie: must be a full qualified address). If you do this, you will need to set a flag once the thread was started and lock such that the flag read/write access is synchronized.
David
@David, interesting approach, that may just work. Thank you!
Sunday Ironfoot