Search code examples
c#asp.nethttpcontext

Determine the URL hostname without using HttpContext.Current?


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 a SqlDependency in the onChange callback for when a SQL Dependency is found. Althougth the code resides in my web app, there is no request, and HttpContext.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?


Solution

  • 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);
    });