简体   繁体   English

如何在WCF自动发现中获取IP地址

[英]How to get IP address in WCF Auto Discovery

I added following function to auto discovery the WCF service in intranet. 我添加了以下功能来自动发现Intranet中的WCF服务。

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 . 在测试过程中,返回地址为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. 尽管我可以使用Dns.GetHostAddress来获取IP地址,但是由于DNS问题,它在本地Intranet中将花费很长时间。
Is there anyway to get the ip address directly during discovery? 无论如何,在发现过程中是否可以直接获取IP地址?

I think your idea is the best solution, by using DNS.GetHostAddress to get the practical IP address of the server. 我认为您的想法是最好的解决方案,通过使用DNS.GetHostAddress来获取服务器的实际IP地址。 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. DiscoveryClient仅返回服务器端定义的服务端点地址,该地址仅适用于控制台应用程序托管的服务。

<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. 对于IIS中托管的服务,无论站点绑定的类型如何,都仅返回域名。 Under this circumstance, only we can use is make use of the DNS. 在这种情况下,只有我们可以使用的是利用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. 请随时告诉我是否有什么我可以帮助的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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