How to: Configure a Custom WS-Metadata Exchange Binding

This topic will explain how to configure a custom WS-Metadata exchange binding. Windows Communication Foundation (WCF) includes four system-defined metadata bindings, but you can publish metadata using any binding you want. This topic will show you how to publish metadata using the wsHttpBinding. This binding gives you the option of exposing metadata in a secure way. The code in this article is based on the Getting Started Sample.

Using a configuration file

  1. In the service's configuration file, add a service behavior that contains the serviceMetadata tag:

    <behaviors>
       <serviceBehaviors>
         <behavior name="CalculatorServiceBehavior">
           <serviceMetadata httpGetEnabled="True"/>
         </behavior>
       </serviceBehaviors>
    </behaviors>
    
  2. Add a behaviorConfiguration attribute to the service tag that references this new behavior:

    <service        name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior"> 
    
  3. Add a metadata endpoint specifying mex as the address, wsHttpBinding as the binding, and IMetadataExchange as the contract:

    <endpoint address="mex"
              binding="wsHttpBinding"
              contract="IMetadataExchange" />
    
  4. To verify the metadata exchange endpoint is working correctly add an endpoint tag in the client configuration file:

    <endpoint name="MyMexEndpoint"               address="https://localhost:8000/servicemodelsamples/service/mex"
              binding="wsHttpBinding"
              contract="IMetadataExchange"/>
    
  5. In the client's Main() method, create a new MetadataExchangeClient instance, set its ResolveMetadataReferences property to true, call GetMetadata and then iterate through the collection of metadata returned:

    string mexAddress = "https://localhost:8000/servicemodelsamples/service/mex";
    
    MetadataExchangeClient mexClient = new MetadataExchangeClient("MyMexEndpoint");
    mexClient.ResolveMetadataReferences = true;
    MetadataSet mdSet = mexClient.GetMetadata(new EndpointAddress(mexAddress));
    foreach (MetadataSection section in mdSet.MetadataSections)
    Console.WriteLine("Metadata section: " + section.Dialect.ToString());
    

Configuring by code

  1. Create a WsHttpBinding binding instance:

    WSHttpBinding binding = new WSHttpBinding();
    
  2. Create a ServiceHost instance:

    ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
    
  3. Add a service endpoint and add a ServiceMetadataBehavior instance:

    serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, baseAddress);
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    serviceHost.Description.Behaviors.Add(smb);
    
  4. Add a metadata exchange endpoint, specifying the WSHttpBinding created earlier:

    serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, mexAddress);
    
  5. To verify that the metadata exchange endpoint is working correctly, add an endpoint tag in the client configuration file:

    <endpoint name="MyMexEndpoint"               address="https://localhost:8000/servicemodelsamples/service/mex"
              binding="wsHttpBinding"
              contract="IMetadataExchange"/>
    
  6. In the client's Main() method, create a new MetadataExchangeClient instance, set the ResolveMetadataReferences property to true, call GetMetadata and then iterate through the collection of metadata returned:

    string mexAddress = "https://localhost:8000/servicemodelsamples/service/mex";
    
    MetadataExchangeClient mexClient = new MetadataExchangeClient("MyMexEndpoint");
    mexClient.ResolveMetadataReferences = true;
    MetadataSet mdSet = mexClient.GetMetadata(new EndpointAddress(mexAddress));
    foreach (MetadataSection section in mdSet.MetadataSections)
    Console.WriteLine("Metadata section: " + section.Dialect.ToString());
    

See Also

Tasks

Metadata Publishing Behavior
Retrieve Metadata

Concepts

Metadata
Publishing Metadata
Publishing Metadata Endpoints