我在学习这一章节的时候着重验证了下通信时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要匹配
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 ServiceHost (depending on the binding protocol) is used as the endpoint's base address.来自MSDN