寻址
“寻址”示例演示终结点地址的各个方面和功能。本示例基于入门示例。在本示例中,服务是自承载的。服务和客户端都是控制台应用程序。服务使用相对和绝对终结点地址的组合来定义多个终结点。
提示
本主题的最后介绍了此示例的设置过程和生成说明。
服务配置文件指定一个基址和四个终结点。基址是在 service/host/baseAddresses 下使用 add 元素来指定的,如下面的示例配置所示。
<service
name="Microsoft.ServiceModel.Samples.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="https://localhost:8000/ServiceModelSamples/service"/>
</baseAddresses>
</host>
...
</service>
在以下示例配置中显示的第一个终结点定义指定一个相对地址,这意味着终结点地址是遵循 URI 构成规则的基址和相对地址的结合。
<!-- Empty relative address specified:
use the base address provided by the host. -->
<!-- The endpoint address is https://localhost:8000/ServiceModelSamples/service. -->
<endpoint address=""
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
在本例中,相对地址为空(“”),因此终结点地址与基址相同。实际终结点地址为 https://localhost:8000/servicemodelsamples/service。
第二个终结点定义也指定一个相对地址,如下面的示例配置所示。
<!-- The relative address specified: use the base address -->
<!-- provided by the host + path. The endpoint address is -->
<!-- https://localhost:8000/servicemodelsamples/service/test. -->
<endpoint address="/test"
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
将相对地址“test”追加到基址。实际终结点地址为 https://localhost:8000/servicemodelsamples/service/test。
第三个终结点定义指定一个绝对地址,如下面的示例配置所示。
<endpoint address="https://localhost:8001/hello/servicemodelsamples"
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
基址在地址中不起作用。实际终结点地址为 https://localhost:8001/hello/servicemodelsamples。
第四个终结点地址指定一个绝对地址和一个不同的传输协议 (TCP)。基址在地址中不起作用。实际终结点地址为 net.tcp://localhost:9000/servicemodelsamples/service。
<!-- The absolute address specified, different transport: -->
<!-- use the specified address, and ignore the base address. -->
<!-- The endpoint address is -->
<!-- net.tcp://localhost:9000/servicemodelsamples/service. -->
<endpoint address=
"net.tcp://localhost:9000/servicemodelsamples/service"
binding="netTcpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
</service>
客户端只访问四个服务终结点之一,但是在客户端的配置文件中对所有四个终结点都进行了定义。客户端创建 CalculatorProxy
对象时将选择一个终结点。通过更改从 CalculatorEndpoint1
到 CalculatorEndpoint4
中的配置名称,您可以使用每个终结点。
运行示例时,服务会枚举其每个终结点的地址、绑定名称和协定名称。从 ServiceHost 的角度来看,元数据交换 (MEX) 终结点只是另一个终结点,因此在列表中出现。
Service endpoints:
Endpoint - address: https://localhost:8000/ServiceModelSamples/service
binding: WSHttpBinding
contract: ICalculator
Endpoint - address: https://localhost:8000/ServiceModelSamples/service/test
binding: WSHttpBinding
contract: ICalculator
Endpoint - address: https://localhost:8001/hello/servicemodelsamples
binding: WSHttpBinding
contract: ICalculator
Endpoint - address: net.tcp://localhost:9000/servicemodelsamples/service
binding: NetTcpBinding
contract: ICalculator
Endpoint - address: https://localhost:8000/ServiceModelSamples/service/mex
binding: MetadataExchangeHttpBinding
contract: IMetadataExchange
The service is ready.
Press <ENTER> to terminate service.
运行客户端时,操作请求和响应将显示在服务和客户端控制台窗口中。在每个控制台窗口中按 Enter 可以关闭服务和客户端。
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Press <ENTER> to terminate client.
设置、生成和运行示例
若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。
若要用单机配置或跨计算机配置来运行示例,请按照运行 Windows Communication Foundation 示例中的说明进行操作。
提示
如果使用 Svcutil.exe 为此示例重新生成配置,请确保在客户端配置中修改终结点名称以与客户端代码匹配。
Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.