我學WCF之旅(二)

來源:互聯網
上載者:User

原文:Endpoint Overview

 

我在學習這一章節的時候著重驗證了下通訊時Endpoint的Address和Binding必須匹配才能夠通訊。

我沒有原作者提供的Sample,而是用了上次的Solution.

修改如下:

1.在Hosting 項目裡加了Address,變成 

Uri  httpBaseAddress = new Uri(http://localhost:8888/generalCalculator); 

Uri tcpBaseAddress = new Uri("net.tcp://localhost:9999/generalCalculator"); 

當然了不同的地址需要的Binding 也不一樣:  

BasicHttpBinding httpbinding = new BasicHttpBinding();  

NetTcpBinding tcpbinging = new NetTcpBinding();

host.AddServiceEndpoint(

typeof(ICalculator), httpbinding, string.Empty); typeof(ICalculator), tcpbinging, string.Empty);

host.AddServiceEndpoint(

儲存->Build->OPen Folder in Windows Explorer -> using Administrator to run Hosting.exe in bin/debug.

 

2.Client項目

我們已經在代碼中更新了Service,這裡也需要更新你的Service Reference 裡的Service.

因為得我連接埠該了,所以我直接刪了原來的Service Reference ,新添加了一個。但是在我新加的時候

在Add Service Reference dialog裡的Address欄裡,我試了一下幾種

(1) http://localhost:8888/generalCalculator   能找到,可以添加

(2) net.tcp://localhost:9999/generalCalculator   不能找到

(3) http://localhost:8887/generalCalculator 不能找到

(4) http://localhost/generalCalculator  不能找到

(5) http://localhost  不能找到

 

裡面應該有什麼規則,還不知道,後續..

 

我可以通過方案1找到我的Service,就在Client裡寫了2種Code 來Invoke 這個Service。

需要注意的地方:Binding 和Address要匹配 

 using (CalculatorServiceClient pp = new CalculatorServiceClient(new BasicHttpBinding(),new EndpointAddress("http://localhost:8888/generalCalculator")))<br /> {<br /> Console.WriteLine("{0} + {1}={2}",1,2,pp.Add(1,2));<br /> Console.WriteLine("{0} + {1}={2}", 1, 2, pp.Subtract(1,2));<br /> Console.WriteLine("{0} + {1}={2}", 1, 2, pp.Multiply(1,2));<br /> Console.WriteLine("{0} + {1}={2}", 1, 2, pp.Divide(4,2));</p><p> }<br /> using (ChannelFactory<ICalculator> cf = new ChannelFactory<ICalculator>(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9999/generalCalculator")))<br /> {<br /> ICalculator pp = cf.CreateChannel();<br /> using (pp as IDisposable)<br /> {<br /> Console.WriteLine("{0} + {1}={2}", 1, 2, pp.Add(1, 2));<br /> Console.WriteLine("{0} + {1}={2}", 1, 2, pp.Subtract(1, 2));<br /> Console.WriteLine("{0} + {1}={2}", 1, 2, pp.Multiply(1, 2));<br /> Console.WriteLine("{0} + {1}={2}", 1, 2, pp.Divide(4, 2));</p><p> }<br /> }

  

 

 string.Empty*這裡的含義,The address for the endpoint added. This can be an absolute or relative URI. If it is a relative URI, one of the base address of the {
Track('ctl00_MTCS_main_ctl42_ctl00_contenthere|ctl00_MTCS_main_ctl42_ctl00_ctl09',this);
}" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx">ServiceHost (depending on the binding protocol) is used as the endpoint's base address.來自MSDN


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.