入门代码教程第五节 如何:配置客户端

如何:配置基本 Windows Communication Foundation 客户端

配置客户端包括指定客户端用于访问服务的终结点。 每个终结点都有一个地址、一个绑定和一个协定,所有这些元素都必须在配置客户端的过程中指定。

在过程后面的示例中提供了为客户端生成的配置文件的内容。

配置 Windows Communication Foundation 客户端

  • 在 Visual Studio 中,将在前一过程如何:创建 Windows Communication Foundation 客户端中生成的 App.config 配置文件添加到客户端项目中。 在“解决方案资源管理器”中右击该客户端,选择“添加现有项”,然后从 C:\Documents and Settings\<用户名>\Documents\Visual Studio 2008\Projects\Service\Client\bin 目录中选择 App.config 配置文件 (之所以命名为 App.config 文件,是因为在使用 Svcutil.exe 工具生成此文件时使用了 /config:app.config 开关)。

  • 打开生成的配置文件。 Svcutil.exe 会为绑定上的每一项设置都生成值。 下面的示例显示了生成的配置文件。 请在 <system.serviceModel> 节下查找 <endpoint> 元素。 下面的配置文件是所生成的文件的简化版本。

  •  

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      
    <system.serviceModel>
        
    <bindings>
          
    <wsHttpBinding>
            
    <binding name="WSHttpBinding_ICalculator">
            
    </binding>
          
    </wsHttpBinding>
        
    </bindings>
        
    <client>
          
    <endpoint
               address
    ="http://localhost:8000/ServiceModelSamples/Service/CalculatorService"
               binding
    ="wsHttpBinding"
               bindingConfiguration
    ="WSHttpBinding_ICalculator"
               contract
    ="Microsoft.ServiceModel.Samples.ICalculator"
               name
    ="WSHttpBinding_ICalculator">
          
    </endpoint>
        
    </client>
      
    </system.serviceModel>
    </configuration> 

     

    此示例配置的终结点可供客户端在访问位于以下地址的服务时使用:http://localhost:8000/ServiceModelSamples/service

    终结点元素指定 Microsoft.ServiceModel.Samples.ICalculator 协定将用于通过系统提供的 WsHttpBinding 配置的通信。 此绑定指定 HTTP 作为传输协议、可互操作安全性以及其他配置详细信息。

    创建和配置了客户端后,下一步是使用客户端来访问服务。

    posted @ 2008-09-25 11:50  木饭  阅读(194)  评论(0编辑  收藏  举报