ASP.NET - Get website's URL without HttpContext.Current (running in background thread)

风流意气都作罢 提交于 2019-12-24 04:00:50

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3398717/asp-net-get-websites-url-without-httpcontext-current-running-in-background-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!