Search code examples
vb.netwcfiishostingvirtual-directory

How to get the virtual path to where WCF is hosted on IIS rather than the physical path?


I have a WCF Service hosted on IIS server, however i try to get the path to which the service is hosted on the IIS, that is similar to: 192.168.1.xx/WCFHosted/VirtualDirectory, by using the below code:

System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath

The above returns a path to the Physical path corresponding to where the IIS Hosted WCF is originally linked from, that is Ex: D/MyService/WCFHosted,

How can i return the IIS Hosted path for the WCF,

Please Advise, Thanks in Advance


Solution

  • Try the below code snippets.

    System.Environment.MachineName+"/" + System.Web.Hosting.HostingEnvironment.SiteName +System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
    

    We can convert the Machine name to an IP address by using DNS.GetHostAddress(hostname) method.

    var result = Dns.GetHostAddresses(Environment.MachineName).ToList();
                foreach (var item in result)
                {
                    Console.WriteLine(item);
                }
    

    Feel free to let me know if there is anything I can help with.