大网数据广告活动
.NET 与 C# 开发

寻址

· 52CE 我爱测

“寻址”示例演示终结点地址的各个方面和功能。 该示例基于Getting Started。 在本示例中,服务是自承载的。 服务和客户端都是控制台应用程序。 服务使用相对和绝对终结点地址的组合来定义多个终结点。

“寻址”示例演示终结点地址的各个方面和功能。 该示例基于Getting Started。 在本示例中,服务是自承载的。 服务和客户端都是控制台应用程序。 服务使用相对和绝对终结点地址的组合来定义多个终结点。

Note: 本主题的最后介绍了此示例的设置过程和生成说明。

服务配置文件指定一个基址和四个终结点。 基址是在 service/host/baseAddresses 下使用 add 元素来指定的,如下面的示例配置所示。

<service name="Microsoft.ServiceModel.Samples.CalculatorService"  
         behaviorConfiguration="CalculatorServiceBehavior">  
  <host>  
    <baseAddresses>  
      <add baseAddress="http://localhost:8000/ServiceModelSamples/service" />  
    </baseAddresses>  
  </host>  
</service>  

在以下示例配置中显示的第一个终结点定义指定一个相对地址,这意味着终结点地址是遵循 URI 构成规则的基址和相对地址的结合。

<!-- Empty relative address specified:   
     use the base address provided by the host. -->  
<!-- The endpoint address is  
     http://localhost:8000/ServiceModelSamples/service. -->  
<endpoint address=""  
          binding="wsHttpBinding"  
          contract="Microsoft.ServiceModel.Samples.ICalculator" />  

在本例中,相对地址为空(“”),因此终结点地址与基址相同。 实际的终结点地址是http://localhost:8000/servicemodelsamples/service。

第二个终结点定义也指定一个相对地址,如下面的示例配置所示。

<!-- The relative address specified: use the base address -->  
<!-- provided by the host + path. The endpoint address is -->  
<!-- http://localhost:8000/servicemodelsamples/service/test. -->  
<endpoint address="/test"  
          binding="wsHttpBinding"  
          contract="Microsoft.ServiceModel.Samples.ICalculator" />  

将相对地址“test”追加到基址。 实际的终结点地址是http://localhost:8000/servicemodelsamples/service/test。

第三个终结点定义指定一个绝对地址,如下面的示例配置所示。

<endpoint address="http://localhost:8001/hello/servicemodelsamples"  
          binding="wsHttpBinding"  
          contract="Microsoft.ServiceModel.Samples.ICalculator" />  

基址在地址中不起作用。 实际的终结点地址是http://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:  http://localhost:8000/ServiceModelSamples/service  
           binding:  WSHttpBinding  
           contract: ICalculator  
Endpoint - address:  http://localhost:8000/ServiceModelSamples/service/test  
           binding:  WSHttpBinding  
           contract: ICalculator  
Endpoint - address:  http://localhost:8001/hello/servicemodelsamples  
           binding:  WSHttpBinding  
           contract: ICalculator  
Endpoint - address:  net.tcp://localhost:9000/servicemodelsamples/service  
           binding:  NetTcpBinding  
           contract: ICalculator  
Endpoint - address:  http://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.  

设置、生成和运行示例

  1. 请确保您具有执行的 Windows Communication Foundation 示例的一次性安装过程。

  2. 若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照 Building the Windows Communication Foundation Samples中的说明进行操作。

  3. 若要在单或跨计算机配置中运行示例,请按照中的说明运行 Windows Communication Foundation 示例。

    NOTE 如果使用 Svcutil.exe 为此示例重新生成配置,请确保在客户端配置中修改终结点名称以与客户端代码匹配。

Important: 您的计算机上可能已安装这些示例。 在继续操作之前,请先检查以下(默认)目录:

<InstallDrive>:\WF_WCF_Samples

如果此目录不存在,请转到Windows Communication Foundation (WCF) 和.NET Framework 4 的 Windows Workflow Foundation (WF) 示例若要下载所有 Windows Communication Foundation (WCF) 和WF示例。 此示例位于以下目录:

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Services\Addressing

历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站展开固定版本仓库内的引用内容,将 Markdown 转为 HTML,规范化链接,移除外部媒体、发布器占位标签和导航标记。

资料来源与适用范围

资料来源
Microsoft / .NET 中文文档历史快照
适用范围
历史中文快照 2019-08-18;详见原文产品版本;文档日期 03/30/2017
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。