Compartilhar via


Metadata Publishing Behavior

Download sample

The Metadata Publishing Behavior sample demonstrates how to control the metadata publishing features of a service. To prevent unintentional disclosure of potentially sensitive service metadata, the default configuration for Windows Communication Foundation (WCF) services disables metadata publishing. This behavior is secure by default, but also means that you cannot use a metadata import tool (such as Svcutil.exe) to generate the client code required to call the service unless the service’s metadata publishing behavior is explicitly enabled in configuration.

Note

The WCF samples may already be installed on your machine. Check this (default) directory before continuing: <InstallDrive>:\Samples\WCFWFCardspaceIf this directory doesn’t exist, click the download sample link at the top of this page. Note that this will download and install all of the WF, WCF, and CardSpace samples, you will only have to do this once. The sample is located in the following directory <InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Service\Behaviors\Metadata.

Note

For clarity, this sample demonstrates how to create an unsecured metadata publishing endpoint. Such endpoints are potentially available to anonymous unauthenticated consumers and care must be taken before deploying such endpoints to ensure that publicly disclosing a service’s metadata is appropriate. See the Custom Secure Metadata Endpoint sample for a sample that secures a metadata endpoint.

The sample is based on the Getting Started Sample, which implements the ICalculator service contract. In this sample, the client is a console application (.exe) and the service is hosted by Internet Information Services (IIS).

Note

The setup procedure and build instructions for this sample are located at the end of this topic.

For a service to expose metadata, the ServiceMetadataBehavior must be configured on the service. When this behavior is present, you can publish metadata by configuring an endpoint to expose the IMetadataExchange contract as an implementation of a WS-MetadataExchange (MEX) protocol. As a convenience, this contract has been given the abbreviated configuration name of "IMetadataExchange". This sample uses the mexHttpBinding, which is a convenience standard binding that is equivalent to the wsHttpBinding with the security mode set to None. A relative address of "mex" is used in the endpoint, which when resolved against the services base address results in an endpoint address of https://localhost/servicemodelsamples/service.svc/mex. The following shows the behavior configuration:

  <behaviors>
    <serviceBehaviors>
      <behavior name="CalculatorServiceBehavior">
        <!-- The serviceMetadata behavior publishes metadata through 
             the IMetadataExchange contract. When this behavior is 
             present, you can expose this contract through an endpoint 
             as shown below. Setting httpGetEnabled to true publishes 
             the service's WSDL at the <baseaddress>?wsdl, for example,
             https://localhost/servicemodelsamples/service.svc?wsdl -->
        <serviceMetadata httpGetEnabled="True"/>
        <serviceDebug includeExceptionDetailInFaults="False" />
      </behavior>
    </serviceBehaviors>
  </behaviors>

The following shows the MEX endpoint.

      <!-- the MEX endpoint is exposed at 
           https://localhost/servicemodelsamples/service.svc/mex 
           To expose the IMetadataExchange contract, you 
           must enable the serviceMetadata behavior as demonstrated                         
           previously. -->
      <endpoint address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />

This sample sets the HttpGetEnabled property to true, which also exposes the service's metadata using HTTP GET. To enable an HTTP GET metadata endpoint, the service must have an HTTP base address. The query string ?wsdl is used on the base address of the service to access the metadata. For example, to see the WSDL for the service in a Web browser you would use the address https://localhost/servicemodelsamples/service.svc?wsdl. Alternatively, you can use this behavior to expose metadata over HTTPS by setting HttpsGetEnabled to true. This requires an HTTPS base address.

To access the service's MEX endpoint use the Service Model Metadata Utility Tool (Svcutil.exe).

svcutil.exe /n:"http://Microsoft.ServiceModel.Samples,Microsoft.ServiceModel.Samples" https://localhost/servicemodelsamples/service.svc/mex /out:generatedClient.cs

This generates a client based on the service's metadata.

To access the service's metadata using HTTP GET, point your browser to https://localhost/servicemodelsamples/service.svc?wsdl.

If you remove this behavior and try to open the service you get an exception. This error occurs because without the behavior, the endpoint configured with the IMetadataExchange contract has no implementation.

If you set HttpGetEnabled to false, you see the CalculatorService help page instead of seeing the service's metadata.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

  4. To find the Metadata Publishing Behavior sample browse to the directory where you installed the WCF samples and browse to ... \WCFWFCardSpace\WCF\Extensibility\Metadata.

© 2007 Microsoft Corporation. All rights reserved.