简体   繁体   中英

How to get IP address in WCF Auto Discovery

I added following function to auto discovery the WCF service in intranet.

private void AutoDiscovery(FindCriteria cirteria)
{
    try
    {
        UdpDiscoveryEndpoint udp = new UdpDiscoveryEndpoint();
        using (DiscoveryClient discoveryClient = new DiscoveryClient(udp))
        {
            cirteria.Duration = TimeSpan.FromSeconds(5);
            FindResponse response = discoveryClient.Find(cirteria);
            if (response.Endpoints.Count > 0)
            {
                foreach (EndpointDiscoveryMetadata point in response.Endpoints)
                {
                    string address = point.Address.Uri.ToString();
                    // net.tcp//computer1:8081/wcfService
                }
            }
        }
    }
    catch(Exception e)
    {

    }
}

During test, the return address is net.tcp//computer1:8081/wcfService . Although I could use Dns.GetHostAddress to get ip address, it will take a long time in local intranet due to DNS issue.
Is there anyway to get the ip address directly during discovery?

I think your idea is the best solution, by using DNS.GetHostAddress to get the practical IP address of the server. It should only be accomplished by the Domain Name System. The DiscoveryClient only return the service endpoint address as the server-side defines, which is only applicable in the service hosted by the console application.

<service name="ConsoleApp3.TestService">
        <!--the below service endpoint address is returned as defined here-->
        <endpoint address="http://10.157.13.69:6666" binding="wsHttpBinding" contract="ConsoleApp3.ITestService" ></endpoint>
        <!--this line code will return domain-->
        <!--<endpoint address="http://vabqia969vm:6666" binding="wsHttpBinding" contract="ConsoleApp3.ITestService"></endpoint>-->
        <!--for exposing the service-->
        <endpoint kind="discoveryEndpoint" address="http://10.157.13.69:6666/exterior" binding="wsHttpBinding" ></endpoint>
      </service>

For the service hosted in IIS, the domain name only is been return regardless of the type of the site binding. Under this circumstance, only we can use is make use of the DNS.

foreach (EndpointDiscoveryMetadata item in response.Endpoints)
            {
                //retrieve IP address
                System.Net.IPHostEntry hostinfo = System.Net.Dns.GetHostEntry(item.Address.Uri.Host);
                string IPAddress = hostinfo.AddressList[2].ToString();
                Console.WriteLine(IPAddress);
            }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM