How to: Publish Metadata for a Service Using a Configuration File
This topic shows how to publish metadata for a Windows Communication Foundation (WCF) service so that clients can retrieve the metadata using a WS-Transfer GET request or an HTTP/GET request using the ?wsdl querystring.
To publish metadata for a WCF service using an application configuration file
-
If it does not exist, create a <behaviors> element that contains a <serviceBehaviors> element.
-
To the <serviceBehaviors> element add a <behavior> element (if one does not exist) and specify a value for the name attribute of the <behavior> element.
-
Then add a <serviceMetadata> element to the <behavior> element, so that it looks like the following code example. (If a <behavior> element already exists, add a <serviceMetadata> element to that one.)
Xml
<behaviors> <serviceBehaviors> <behavior name="metadataSupport"> <!-- Enables the IMetadataExchange endpoint in services that --> <!-- use "metadataSupport" in their behaviorConfiguration attribute. --> <serviceMetadata /> </behavior> </serviceBehaviors> </behaviors>
-
Add a behaviorConfiguration attribute to the <service> element for which you want to publish metadata and specify the name attribute of the <behavior> element added in step 1, as in the following code example.
Xml
<service name="Microsoft.WCF.Documentation.SampleService" behaviorConfiguration="metadataSupport" > <host> <baseAddresses> <add baseAddress="http://localhost:8080/SampleService" /> </baseAddresses> </host>
-
Add one or more <endpoint> elements with the contract set to IMetadataExchange, as in the following code example.
Xml
<!-- Adds a WS-MetadataExchange endpoint at --> <!-- "http://localhost:8080/SampleService/mex" --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
-
For the metadata endpoints added in step 5, set the binding attribute to one of the following:
-
mexHttpBinding for HTTP publication
-
mexHttpsBinding for HTTPS publication
-
mexNamedPipeBinding for named pipe publication
-
mexTcpBinding for TCP publication
-
-
For the metadata endpoints added in step 5, set the address equal to:
-
An empty string to use the host application's base address as the publication point if the base address is the same as the metadata binding.
-
A relative address if the host application has a base address.
-
An absolute address.
-
To enable HTTP/GET metadata requests that use the "?wsdl" querystring convention
-
Perform steps 1 and 2 in the preceding procedure.
-
Then add a <serviceMetadata> element to the <behavior> element and set the <serviceMetadata> element's httpGetEnabled attribute to true and add an empty string to the <serviceMetadata> element's httpGetUrl attribute so that it looks like the following code example.
Xml
<behaviors> <serviceBehaviors> <behavior name="metadataSupport"> <!-- Enables the IMetadataExchange endpoint in services that --> <!-- use "metadataSupport" in their behaviorConfiguration attribute. --> <!-- In addition, the httpGetEnabled and httpGetUrl attributes publish --> <!-- Service metadata for retrieval by HTTP/GET at the address --> <!-- "http://localhost:8080/SampleService?wsdl" --> <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> </behavior> </serviceBehaviors> </behaviors>
Note:
If there is a base address the value of the httpGetUrl attribute can be relative or absolute, and the ?wsdl querystring is automatically appended to it. (In the case of an empty string, the base address is used.) If there is no base address the value must be an absolute address.
-
Add a behaviorConfiguration attribute to the service for which you want to publish metadata and specify the name attribute of the <behavior> element added in step 1, as in the following code example.
Xml
<service name="Microsoft.WCF.Documentation.SampleService" behaviorConfiguration="metadataSupport" > <host> <baseAddresses> <add baseAddress="http://localhost:8080/SampleService" /> </baseAddresses> </host>
Example
The following code example shows the configuration file for a single service that exposes its metadata at http://localhost:8080/SampleService/mex using WS-Transfer GET over HTTP and at http://localhost:8080/SampleService?wsdl using HTTP/GET.
Xml
<configuration> <system.serviceModel> <services> <service name="Microsoft.WCF.Documentation.SampleService" behaviorConfiguration="metadataSupport" > <host> <baseAddresses> <add baseAddress="http://localhost:8080/SampleService" /> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="Microsoft.WCF.Documentation.ISampleService" /> <!-- Adds a WS-MetadataExchange endpoint at --> <!-- "http://localhost:8080/SampleService/mex" --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="metadataSupport"> <!-- Enables the IMetadataExchange endpoint in services that --> <!-- use "metadataSupport" in their behaviorConfiguration attribute. --> <!-- In addition, the httpGetEnabled and httpGetUrl attributes publish --> <!-- Service metadata for retrieval by HTTP/GET at the address --> <!-- "http://localhost:8080/SampleService?wsdl" --> <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
See Also
Reference
Source: How to: Publish Metadata for a Service Using a Configuration File