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
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>
Add a behaviorConfiguration attribute to the service tag that references this new behavior:
<service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
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" />
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"/>
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
Create a WsHttpBinding binding instance:
WSHttpBinding binding = new WSHttpBinding();
Create a ServiceHost instance:
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
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);
Add a metadata exchange endpoint, specifying the WSHttpBinding created earlier:
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, mexAddress);
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"/>
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
Concepts
Metadata in Windows Communication Foundation
Publishing Metadata
Publishing Metadata Endpoints
Other Resources
Metadata Publishing Behavior
Retrieve Metadata
© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07