Normally this isn't a problem because you are working with a request which contains the host name of the current server. I had a unique problem where I was starting some threads from the Global.asax application objects and some of those threads would generate emails that contain click-back links to the site.
#1. I added a public variable to my Global object
public class Global : System.Web.HttpApplication
{
private string _ServerRoot = string.Empty;
public string ServerRoot
{
get { return _ServerRoot; }
}
#2. I added code to the Application_start the filled in _ServerRoot
void Application_Start(object sender, EventArgs e)
{
// create the server root
Uri uriMap = new Uri(Context.Request.Url.AbsoluteUri);
StringBuilder bldNow = new StringBuilder();
bldNow.Append(uriMap.Scheme);
bldNow.Append("://");
bldNow.Append(uriMap.Host);
if (uriMap.Port != 80)
{
bldNow.Append(":");
bldNow.Append(uriMap.Port);
}
_ServerRoot = bldNow.ToString();
#3. I passed the object to my threaded object. I chose this way so I didn't have to worry about context when I need to get the address.
void Application_Start(object sender, EventArgs e)
{
... other init code
// start threads
Friend.Current.Init(this);
#4. I save the class in my threaded object
public void Init(Global global)
{
try
{
CurrentApplication = global;
Init(SystemName, null);
Anytime I need to get access to the server address I just reference CurrentApplication.ServerRoot from inside my thread.
I'm looking for something cleaner, let me know if you've come up with something.
No comments:
Post a Comment