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

这是创建基本 Windows Communication Foundation (WCF) 服务和可以调用该服务的客户端所需的六项任务中的第五项任务。有关全部六项任务的概述,请参见入门教程主题。

本主题要将使用Service Model Metadata Utility (Svcutil.exe) 生成的客户端配置文件添加到客户端项目中,并解释客户端配置元素的内容。配置客户端包括指定客户端用于访问服务的终结点。每个终结点都有一个地址、一个绑定和一个协定,所有这些元素都必须在配置客户端的过程中指定。

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

配置 Windows Communication Foundation 客户端

  1. 在 Visual Studio 中,将在前一过程如何:创建 Windows Communication Foundation 客户端中生成的 App.config 配置文件添加到客户端项目中。在**“解决方案资源管理器”中右击客户端项目,选择“添加”,然后选择“现有项”。然后,从 C:\Users\<用户名>\Documents\Visual Studio 2005\Projects\Service\Client 目录中选择 App.config 配置文件。(之所以命名为 App.config 文件,是因为在使用 Svcutil.exe 工具生成此文件时使用了 /config:app.config 开关)。单击“确定”。默认情况下,“添加现有项”对话框会筛选出具有 .config 扩展名的所有文件。若要查看这些文件,请从位于“添加现有项”对话框右下角的下拉列表框中选择“所有文件(*.*)”**。

  2. 打开生成的配置文件。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="https://localhost:8000/ServiceModelSamples/Service/CalculatorService"
               binding="wsHttpBinding"
               bindingConfiguration="WSHttpBinding_ICalculator"
               contract="Microsoft.ServiceModel.Samples.ICalculator"
               name="WSHttpBinding_ICalculator">
          </endpoint>
        </client>
      </system.serviceModel>
    </configuration> 
    

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

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

  3. 有关 如何在此配置下使用生成的客户端的更多信息,请参见如何:使用 Windows Communication Foundation 客户端

示例

下面的示例演示为客户端生成的配置文件的内容。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator"     
          closeTimeout="00:01:00"
          openTimeout="00:01:00" 
          receiveTimeout="00:10:00" 
          sendTimeout="00:01:00"
          bypassProxyOnLocal="false" 
          transactionFlow="false"  
          hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" 
          maxReceivedMessageSize="65536"
          messageEncoding="Text" 
          textEncoding="utf-8" 
          useDefaultWebProxy="true"
          allowCookies="false">
          <readerQuotas maxDepth="32" 
            maxStringContentLength="8192" 
            maxArrayLength="16384"
            maxBytesPerRead="4096" 
            maxNameTableCharCount="16384" />
          <reliableSession ordered="true" 
            inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" 
              proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" 
              negotiateServiceCredential="true"
              algorithmSuite="Default" 
              establishSecurityContext="true" />
           </security>
      </binding>
    </wsHttpBinding>
  </bindings>
  <client>
    <endpoint address="https://localhost:8000/ServiceModelSamples/Service/CalculatorService"
      binding="wsHttpBinding" 
      bindingConfiguration="WSHttpBinding_ICalculator"
      contract="ICalculator" 
      name="WSHttpBinding_ICalculator">
        <identity>
          <userPrincipalName value="user@contoso.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

现在已配置完客户端。请继续执行如何:使用 Windows Communication Foundation 客户端中的步骤。有关疑难解答信息,请参见入门教程疑难解答

另请参见

任务

如何:创建 Windows Communication Foundation 客户端

概念

使用绑定配置服务和客户端

其他资源

Service Metadata Utility (Svcutil.exe)
Getting Started Sample
Self-Host