Compartilhar via


How to: Retrieve Metadata Over a non-MEX Binding

This topic will describe how to retrieve metadata from a MEX endpoint over a non-MEX binding. The code in this sample is based on the Custom Secure Metadata Endpoint sample.

To retrieve metadata over a non-MEX binding

  1. Determine the binding used by the MEX endpoint. For WCF services, you can determine the MEX binding by accessing the service's configuration file. In this case, the MEX binding is defined in the following service configuration:

    <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. In the client configuration file, configure the same custom binding. Here the client also defines a clientCredentials behavior to provide a certificate to use to authenticate to the service when requesting metadata from the MEX endpoint. When using Svcutil.exe to request metadata over a custom binding, you should add the MEX endpoint configuration to the configuration file for Svcutil.exe (Svcutil.exe.config), and the name of the endpoint configuration should match the URI scheme of the address of the MEX endpoint:

      <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. Create a MetadataExchangeClient and call GetMetadata. There are two ways to do this: you can specify the custom binding in configuration, or you can specify the custom binding in code:

    // 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. Create a WsdlImporter and call ImportAllEndpoints:

    WsdlImporter importer = new WsdlImporter(mexSet);
    ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
    
  5. At this point, you have a collection of service endpoints. For more information about importing metadata, see How to: Import Metadata into Service Endpoints.

See Also

Concepts

Metadata in Windows Communication Foundation


© 2007 Microsoft Corporation. All rights reserved.
Last Published: 2010-03-21