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

  1. If it does not exist, create a <behaviors> element that contains a <serviceBehaviors> element.

  2. 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.

  3. 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

    Copy Code

      <behaviors>
        <serviceBehaviors>
        <behavior name="metadataSupport">
          <!-- Enables the IMetadataExchange endpoint in services that -->
          <!-- use "metadataSupport" in their behaviorConfiguration attribute. -->
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  4. 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

    Copy Code

    <service 
      name="Microsoft.WCF.Documentation.SampleService"
      behaviorConfiguration="metadataSupport"
    >
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8080/SampleService" />
        </baseAddresses>
      </host>
    
  5. Add one or more <endpoint> elements with the contract set to IMetadataExchange, as in the following code example.

    Xml

    Copy Code

    <!-- Adds a WS-MetadataExchange endpoint at -->
    <!-- "http://localhost:8080/SampleService/mex" -->
    <endpoint
       address="mex"
       binding="mexHttpBinding"
       contract="IMetadataExchange"
    />
    
  6. 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

  7. For the metadata endpoints added in step 5, set the address equal to:

    1. 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.

    2. A relative address if the host application has a base address.

    3. An absolute address.

To enable HTTP/GET metadata requests that use the "?wsdl" querystring convention

  1. Perform steps 1 and 2 in the preceding procedure.

  2. 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

    Copy Code

      <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>
    

    NoteNote:

    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.

  3. 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

    Copy Code

    <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

Copy Code

<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

ServiceMetadataBehavior

Source: How to: Publish Metadata for a Service Using a Configuration File

posted on 2007-03-11 18:35  Joey Liang  阅读(431)  评论(0编辑  收藏  举报