如何:通过非 MEX 绑定检索元数据

本主题介绍如何通过非 MEX 绑定从 MEX 终结点检索元数据。本示例中的代码基于自定义安全元数据终结点示例。

通过非 MEX 绑定检索元数据

  1. 确定 MEX 终结点所使用的绑定。对于 Windows Communication Foundation (WCF) 服务,可以通过访问服务的配置文件来确定 MEX 绑定。在本例中,MEX 绑定在以下服务配置中定义。

    <services>
        <service name="Microsoft.ServiceModel.Samples.CalculatorService"
                behaviorConfiguration="CalculatorServiceBehavior">
         <!-- Use the base address provided by the host. -->
         <endpoint address=""
           binding="wsHttpBinding"
           bindingConfiguration="Binding2"
           contract="Microsoft.ServiceModel.Samples.ICalculator" />
         <endpoint address="mex"
           binding="wsHttpBinding"
           bindingConfiguration="Binding1"
           contract="IMetadataExchange" />
         </service>
     </services>
     <bindings>
       <wsHttpBinding>
         <binding name="Binding1">
           <security mode="Message">
             <message clientCredentialType="Certificate" />
           </security>
         </binding>
         <binding name="Binding2">
           <reliableSession inactivityTimeout="00:01:00" enabled="true" />
           <security mode="Message">
             <message clientCredentialType="Certificate" />
           </security>
         </binding>
       </wsHttpBinding>
     </bindings>
    
  2. 在客户端配置文件中,配置同样的自定义绑定。在该配置文件中,客户端还定义了一个 clientCredentials 行为以提供证书,用于在请求 MEX 终结点中的元数据时对服务进行身份验证。在使用 Svcutil.exe 通过自定义绑定请求元数据时,应向 Svcutil.exe 的配置文件 (Svcutil.exe.config) 中添加 MEX 终结点配置,并且终结点配置的名称应与 MEX 终结点地址的 URI 架构匹配,如下面的代码所示。

      <system.serviceModel>
        <client>
          <endpoint name="http"
                    binding="wsHttpBinding"
                    bindingConfiguration="Binding1"
                    behaviorConfiguration="ClientCertificateBehavior"
                    contract="IMetadataExchange" />
        </client>
        <bindings>
          <wsHttpBinding>
            <binding name="Binding1">
              <security mode="Message">
                <message clientCredentialType="Certificate"/>
              </security>
            </binding>
          </wsHttpBinding>
        </bindings>
        <behaviors>
          <endpointBehaviors>
            <behavior name="ClientCertificateBehavior">
              <clientCredentials>
                <clientCertificate findValue="client.com" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" />
                <serviceCertificate>
                  <authentication certificateValidationMode="PeerOrChainTrust" />
                </serviceCertificate>
              </clientCredentials>
            </behavior>
          </endpointBehaviors>
        </behaviors>  
      </system.serviceModel>
    
  3. 创建一个 MetadataExchangeClient 并调用 GetMetadata。有两种方法可以完成此操作:在配置中指定自定义绑定,或在代码中指定自定义绑定,如下面的示例所示。

    // The custom binding is specified in configuration.
    EndpointAddress mexAddress = new EndpointAddress("https://localhost:8000/ServiceModelSamples/Service/mex");
    
    MetadataExchangeClient mexClient = new MetadataExchangeClient("http");
    mexClient.ResolveMetadataReferences = true;
    MetadataSet mexSet = mexClient.GetMetadata(mexAddress);
    
    // The custom binding is specified in code.
    // Specify the Metadata Exchange binding and its security mode.
    WSHttpBinding mexBinding = new WSHttpBinding(SecurityMode.Message);
    mexBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
    
    // Create a MetadataExchangeClient and set the certificate details.
    MetadataExchangeClient mexClient = new MetadataExchangeClient(mexBinding);
    mexClient.SoapCredentials.ClientCertificate.SetCertificate(
        StoreLocation.CurrentUser, StoreName.My,
        X509FindType.FindBySubjectName, "client.com");
    mexClient.SoapCredentials.ServiceCertificate.Authentication.    CertificateValidationMode =    X509CertificateValidationMode.PeerOrChainTrust;
    mexClient.SoapCredentials.ServiceCertificate.SetDefaultCertificate(
        StoreLocation.CurrentUser, StoreName.TrustedPeople,
        X509FindType.FindBySubjectName, "localhost");
    MetadataExchangeClient mexClient2 = new MetadataExchangeClient(customBinding);
    mexClient2.ResolveMetadataReferences = true;
    MetadataSet mexSet2 = mexClient2.GetMetadata(mexAddress);
    
  4. 创建 WsdlImporter 并调用 ImportAllEndpoints,如下面的代码所示。

    WsdlImporter importer = new WsdlImporter(mexSet);
    ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
    
  5. 此时,您拥有服务终结点的集合。有关导入元数据的更多信息,请参见如何:将元数据导入服务终结点

另请参见

概念

元数据