原文: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