Determine the URL hostname without using HttpContext.Current?
Solution 1
If you know the host at the moment you're setting up the event handler then you should be able to do something like (code not actually tested):
string host = HttpContext.Current.Request.Url.Host;
var dep = new SqlDependency(cmd);
dep.OnChange += ((sender, args) =>
{
DoStuff(host);
});
Solution 2
If you are running this from a web application, and it is all managed code then HttpContext must exist. Does your child library (assuming your managed code is in a library) have a reference to System.Web? If not, consider adding this reference. From that point you should be able to access the HttpContext directly by using the fully qualified namespace:
System.Web.HttpContext.Current.Request.Url.Host
In any case, unless your code is unmanaged or your context truly does not originate with a web application, HttpContext should be available at every point while the thread is alive.
Edit:
Based on reading your comment below, it sounds like the SqlDependency is being fired independently. While it's on the same thread, it's not being fired directly by the request. Since all you're looking for is the host url, it's not inconceivable that you can create an application variable or a static variable to hold this information in the event that it is needed for a dependency.
Also something I have seen is that while HttpContext.Current
may not be available, HttpContext.Request
might be. These should be the same object, but they may not necessarily be. It's possible that the Host may be found there.
Solution 3
How about
Environment.MachineName
Solution 4
You should use the IIS api to query the information from the website you're looking for. Because depending on the IIS configuration your URL or Hostname could be differing. (Think about hostheaders, ports, protocols and stuff like this.
A introduction for IIS API could be found at http://learn.iis.net/page.aspx/165/how-to-use-microsoftwebadministration/
Matt Roberts
Updated on January 13, 2020Comments
-
Matt Roberts over 4 years
Using the current request I can get the URL hostname with:
HttpContext.Current.Request.Url.Host
But - I need to determine the URL hostname without using the current request (
HttpContext.Current
). The reason for this is that my code is called from aSqlDependency
in the onChange callback for when a SQL Dependency is found. Althougth the code resides in my web app, there is no request, andHttpContext.Current
is null.I was hoping I could grab it from
HttpRuntime
, but there doesn't seem to be anything of use there. is there a way I can get this information?-
spender over 12 yearsOne of our web sites responds to any supplied host name. Without inspecting the incoming request, it would be difficult to ascertain the hostname used to reach us.
-
Journey over 12 yearsWhat are you trying to do, and exactly when is your code being run? Is it in the SqlDependency's OnChange event, in the App Cache's onRemoveCallback or somewhere else?
-
Matt Roberts over 12 yearsEdited the question for clarity
-
-
Matt Roberts over 12 yearsSorry, this won't work, I have multiple instances on the same box, with different host bindings (site1.xxx.com, site2.xxx.com etc)
-
Wiktor Zychla over 12 yearsI wonder whether it is possible then to determine the context under which your code executes if the HttpContext is missing (as you said).
-
Matt Roberts over 12 yearsThis looks like the only way to do it, however, it requires extra permissions to interrogate IIS, so I think I may need to see if I can code round this issue.
-
Matt Roberts over 12 yearsHttpContext exists, but HttpContext.Current (which is the current request) does not, because the code in the web app is not triggered by a request, its triggered by the SQL Dependency callback, hence it's null.
-
jrummell over 12 yearsCan you set a Page field to
HttpContext.Current
(e.g. _currentContext) and access it in the callback? -
Matt Roberts over 12 yearsThanks. A static variable would work except for the rare case where the app pool recycles and no requets have been made (that would set the static variable) before the sql dependency event fires.
-
Matt Roberts over 12 yearsCheers, I was coming round to this too, so assuming I can move my code out of Application_OnStart to something like Session_OnStart (to make the Request object avaialable), I can pass in the host as you suggest.