如何:通过自定义绑定检索元数据

本主题说明如何通过自定义绑定检索 MEX 终结点中的元数据。本示例中的代码基于Custom Secure Metadata Endpoint示例。

通过自定义绑定检索元数据

  1. 确定由 MEX 终结点使用的绑定。对于 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 通过自定义绑定请求元数据时,应该将 MEX 终结点配置添加到 Svcutil.exe 的配置文件 (Svcutil.exe.config) 中,并且终结点配置的名称应该与 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. 此时,您拥有服务终结点的集合。有关导入元数据的更多信息,请参见如何:将元数据导入服务终结点

另请参见

概念

元数据