亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 WCF 自動發現中獲取 IP 地址

如何在 WCF 自動發現中獲取 IP 地址

C#
互換的青春 2023-09-16 17:50:21
我添加了以下功能來自動發現內網中的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)    {    }}測試時,返回地址為net.tcp//computer1:8081/wcfService。雖然我可以使用它Dns.GetHostAddress來獲取IP地址,但由于DNS問題,在本地內網中需要很長時間。有沒有辦法在發現過程中直接獲取IP地址?
查看完整描述

1 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

我認為你的想法是最好的解決方案,通過使用 DNS.GetHostAddress 來獲取服務器的實際 IP 地址。它只能由域名系統來完成。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>

對于 IIS 中托管的服務,無論站點綁定的類型如何,都僅返回域名。這種情況下,我們就只能利用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);

            }

如果有什么需要我幫忙的,請隨時告訴我。


查看完整回答
反對 回復 2023-09-16
  • 1 回答
  • 0 關注
  • 125 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號