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

This is one of two How To topics that demonstrates publishing metadata for a Windows Communication Foundation (WCF) service. There are two ways to specify how a service should publish metadata, using a configuration file and using code. This topic shows how to publish metadata for a service using a configuration file.

Note

This topic shows how to publish metadata in an unsecure manner. Any client can retrieve the metadata from the service. If you require your service to publish metadata in a secure fashion see Custom Secure Metadata Endpoint.

For more information about publishing metadata in code, see How to: Publish Metadata for a Service Using Code. Publishing metadata allows clients to retrieve the metadata using a WS-Transfer GET request or an HTTP/GET request using the ?wsdl query string. To be sure that the code is working, create a basic WCF service. For simplicity a basic self-hosted service is provided in the following code.

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Metadata.Samples
{
    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract]
        string SimpleMethod(string msg);
    }

    class SimpleService : ISimpleService
    {
        public string SimpleMethod(string msg)
        {
            Console.WriteLine("The caller passed in " + msg);
            return "Hello " + msg;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(SimpleService)); 
            try
            {
                // Open the service host to accept incoming calls
                host.Open();

                // The service can now be accessed.
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                host.Close();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
            }
        }
    }
}

This service is a self-hosted service which is configured using a configuration file. The following configuration file serves as a starting point for this topic.

<configuration>
  <system.serviceModel>
    <services>
      <service name="Metadata.Example.SimpleService">

        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:8001/MetadataSample"/>
          </baseAddresses>
        </host>

        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Metadata.Example.ISimpleService" />

      </service>
    </services>
    <behaviors>
      
    </behaviors>
  </system.serviceModel>
</configuration>

To publish metadata for a WCF service using an application configuration file

  1. Within the App.config file, after the closing </services> element, create a <behaviors> element.

    <behaviors>
    
    </behaviors>
    
  2. Within the <behaviors> element, add a <serviceBehaviors> element.

    <behaviors>
      <serviceBehaviors>
    
      </serviceBehaviors>
    </behaviors>
    
  3. Add a <behavior> element to the <serviceBehaviors> element and specify a value for the name attribute of the <behavior> element.

    <behaviors>
      <serviceBehaviors>
        <behavior name="SimpleServiceBehavior">
    
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  4. Add a <serviceMetadata> element to the <behavior> element. Set the httpGetEnabled attribute to true and the policyVersion attribute to Policy15. httpGetEnabled allows the service to respond to metadata requests made by an HTTP GET request. policyVersion tells the service to conform to WS-Policy 1.5 when generating metadata.

    <behaviors>
      <serviceBehaviors>
        <behavior name="SimpleServiceBehavior">
          <serviceMetadata httpGetEnabled="True" policyVersion="Policy15" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  5. Add a behaviorConfiguration attribute to the <service> element and specify the name attribute of the <behavior> element added in step 1, as in the following code example.

    <services>
      <service
          name="Metadata.Example.SimpleService"
          behaviorConfiguration="SimpleServiceBehavior">
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SimpleServiceBehavior">
          <serviceMetadata httpGetEnabled="True" policyVersion="Policy15" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  6. Add one or more <endpoint> elements with the contract set to IMetadataExchange, as in the following code example.

    <services>
      <service
          name="Metadata.Example.SimpleService"
          behaviorConfiguration="SimpleServiceBehavior">
    
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:8001/MetadataSample"/>
          </baseAddresses>
        </host>
    
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Metadata.Example.ISimpleService" />
    
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    
  7. For the metadata endpoints added in step 6, 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.

  8. For the metadata endpoints added in step 6, 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

  9. Build and run the console application.

  10. Use Internet Explorer to browse to the base address of the service (https://localhost:8001/MetadataSample in this sample) and verify that the metadata publishing is turned on. If not, a message at the top of the resulting page displays: "Metadata publishing for this service is currently disabled."

Example

The following code example shows the implementation of a basic WCF service and the configuration file that publishes metadata for the service.

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Metadata.Samples
{
    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract]
        string SimpleMethod(string msg);
    }

    class SimpleService : ISimpleService
    {
        public string SimpleMethod(string msg)
        {
            Console.WriteLine("The caller passed in " + msg);
            return "Hello " + msg;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(SimpleService)); 
            try
            {
                // Open the service host to accept incoming calls
                host.Open();

                // The service can now be accessed.
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                host.Close();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
            }
        }
    }
}
<configuration>
  <system.serviceModel>
    <services>
      <service
          name="Metadata.Samples.SimpleService"
          behaviorConfiguration="SimpleServiceBehavior">

        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:8001/MetadataSample"/>
          </baseAddresses>
        </host>

        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Metadata.Samples.ISimpleService" />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="SimpleServiceBehavior">
          <serviceMetadata httpGetEnabled="True" policyVersion="Policy12" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>
</configuration>

See Also

Tasks

How to: Host a WCF Service in a Managed Application
How to: Publish Metadata for a Service Using Code

Reference

ServiceMetadataBehavior

Concepts

Metadata Architecture Overview
Using Metadata

Other Resources

Self-Host


© 2007 Microsoft Corporation. All rights reserved.
Last Published: 2010-03-21