Search code examples
c#.netiis-7domain-name

Not sure how I can determine my ASP.NET website domain and port, programatically


In my global.asax I'm wiring up some services via AutoFac for DI/IoC.

One of these services would like to know the domain name and port of the current website.

for example:-

builder.RegisterType<AuthenticationService>()
                .As<IAuthenticationService>()
                .WithParameter("localhostServer", new Uri(?????))
                .SingleInstance();

this is some configuration stuff.

I have no idea how to pass in the server ip and port, via a new Uri instance when I'm doing this in my global.asax Application_Start()

Update: Server == IIS7, integrated mode. (not iis6 or classic mode).


Solution

  • I don't think there is a sure way to do this:

    • IIS can map a number of ips, hostnames and ports to a web application
    • Each request can be bound to a different one

    You probably need to do one of these things:

    • change your code so the binding is only needed when you have a request and not at application start
    • if you know the correct value before hand , you can set it by storing it via a configuration key