How to: Retrieve Metadata Over a non-MEX Binding

This topic describes 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 Windows Communication Foundation (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.

        <service name="Microsoft.ServiceModel.Samples.CalculatorService"  
         <!-- Use the base address provided by the host. -->  
         <endpoint address=""  
           contract="Microsoft.ServiceModel.Samples.ICalculator" />  
         <endpoint address="mex"  
           contract="IMetadataExchange" />  
         <binding name="Binding1">  
           <security mode="Message">  
             <message clientCredentialType="Certificate" />  
         <binding name="Binding2">  
           <reliableSession inactivityTimeout="00:01:00" enabled="true" />  
           <security mode="Message">  
             <message clientCredentialType="Certificate" />  
  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, as shown in the following code.

        <endpoint name="http"  
                  contract="IMetadataExchange" />  
          <binding name="Binding1">  
            <security mode="Message">  
              <message clientCredentialType="Certificate"/>  
          <behavior name="ClientCertificateBehavior">  
              <clientCertificate findValue="" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" />  
                <authentication certificateValidationMode="PeerOrChainTrust" />  
  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, as shown in the following example.

    // The custom binding is specified in configuration.  
    EndpointAddress mexAddress = new EndpointAddress("http://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);  
        StoreLocation.CurrentUser, StoreName.My,  
        X509FindType.FindBySubjectName, "");  
        CertificateValidationMode =  
        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, as shown in the following code.

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

See Also