元数据发布行为

“元数据发布行为”示例演示如何控制服务的元数据发布功能。为了防止无意中泄露潜在的敏感服务元数据,Windows Communication Foundation (WCF) 服务的默认配置将禁用元数据发布。默认情况下此行为是安全的,但也意味着您无法使用元数据导入工具(例如 Svcutil.exe)生成调用服务所需的客户端代码,除非在配置中显式启用服务的元数据发布行为。

ms751498.Important(zh-cn,VS.100).gif 注意:
为清楚起见,本示例演示如何创建不安全的元数据发布终结点。此类终结点或许可以供未通过身份验证的匿名使用者使用,因此在部署此类终结点之前,必须谨慎以确保适合公开透露服务元数据。有关确保元数据终结点安全的示例,请参见自定义安全元数据终结点示例。

此示例基于实现 ICalculator 服务协定的入门示例。在此示例中,客户端是一个控制台应用程序 (.exe),服务是由 Internet 信息服务 (IIS) 承载的。

ms751498.note(zh-cn,VS.100).gif注意:
本主题的末尾介绍了此示例的设置过程和生成说明。

若要使服务公开元数据,必须在服务上配置 ServiceMetadataBehavior。当存在此行为时,可以通过如下方法发布元数据:配置一个终结点,将 IMetadataExchange 协定作为 WS-MetadataExchange (MEX) 协议的实现来公开。为方便起见,为此协定指定了缩写的配置名称“IMetadataExchange”。此示例使用的是 mexHttpBinding,这是一种便于使用的标准绑定,与安全模式设置为 NonewsHttpBinding 等效。在终结点中使用相对地址“mex”,当针对服务基址对其进行解析时将得到终结点地址 https://localhost/servicemodelsamples/service.svc/mex。下面演示了行为配置:

  <behaviors>
    <serviceBehaviors>
      <behavior name="CalculatorServiceBehavior">
        <!-- The serviceMetadata behavior publishes metadata through 
             the IMetadataExchange contract. When this behavior is 
             present, you can expose this contract through an endpoint 
             as shown below. Setting httpGetEnabled to true publishes 
             the service's WSDL at the <baseaddress>?wsdl, for example,
             https://localhost/servicemodelsamples/service.svc?wsdl -->
        <serviceMetadata httpGetEnabled="True"/>
        <serviceDebug includeExceptionDetailInFaults="False" />
      </behavior>
    </serviceBehaviors>
  </behaviors>

下面演示了 MEX 终结点。

      <!-- the MEX endpoint is exposed at 
           https://localhost/servicemodelsamples/service.svc/mex 
           To expose the IMetadataExchange contract, you 
           must enable the serviceMetadata behavior as demonstrated                         
           previously. -->
      <endpoint address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />

此示例将 HttpGetEnabled 属性设置为 true,它还使用 HTTP GET 公开服务的元数据。若要启用 HTTP GET 元数据终结点,服务必须具有 HTTP 基址。在服务的基址中使用了查询字符串 ?wsdl 以访问元数据。例如,若要在 Web 浏览器中查看服务的 WSDL,应使用地址 https://localhost/servicemodelsamples/service.svc?wsdl。或者,可以通过将 HttpsGetEnabled 设置为 true,使用此行为通过 HTTPS 公开元数据。这需要一个 HTTPS 基址。

若要访问服务的 MEX 终结点,请使用ServiceModel 元数据实用工具 (Svcutil.exe)

svcutil.exe /n:"http://Microsoft.ServiceModel.Samples,Microsoft.ServiceModel.Samples" https://localhost/servicemodelsamples/service.svc/mex /out:generatedClient.cs

这将根据服务的元数据生成一个客户端。

若要使用 HTTP GET 访问服务的元数据,请将浏览器指向 https://localhost/servicemodelsamples/service.svc?wsdl。

如果移除此行为并尝试打开服务,将引发异常。发生此错误是因为,如果没有该行为,使用 IMetadataExchange 协定配置的终结点则没有实现。

如果将 HttpGetEnabled 设置为 false,您将看到 CalculatorService 帮助页面,而不是服务的元数据。

设置、生成和运行示例

  1. 请确保已经执行了 Windows Communication Foundation 示例的一次性安装过程

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

  3. 若要用单机配置或跨计算机配置来运行示例,请按照Running the Windows Communication Foundation Samples中的说明进行操作。

ms751498.Important(zh-cn,VS.100).gif 注意:
您的计算机上可能已安装这些示例。在继续操作之前,请先检查以下(默认)目录:

<安装驱动器>:\WF_WCF_Samples

如果此目录不存在,请访问针对 .NET Framework 4 的 Windows Communication Foundation (WCF) 和 Windows Workflow Foundation (WF) 示例(可能为英文网页),下载所有 Windows Communication Foundation (WCF) 和 WF 示例。此示例位于以下目录。

<安装驱动器>:\WF_WCF_Samples\WCF\Basic\Services\Behaviors\Metadata