Export (0) Print
Expand All

ServiceMetadataBehavior Class

Controls the publication of service metadata and associated information.

System.Object
  System.ServiceModel.Description.ServiceMetadataBehavior

Namespace:  System.ServiceModel.Description
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public class ServiceMetadataBehavior : IServiceBehavior

The ServiceMetadataBehavior type exposes the following members.

  NameDescription
Public methodServiceMetadataBehaviorInitializes a new instance of the ServiceMetadataBehavior class.
Top

  NameDescription
Public propertyExternalMetadataLocationGets or sets a value that is the location of service metadata.
Public propertyHttpGetBindingGets or sets a binding used to configure metadata retrieval when the transport is HTTP.
Public propertyHttpGetEnabledGets or sets a value that indicates whether to publish service metadata for retrieval using an HTTP/GET request.
Public propertyHttpGetUrlGets or sets the location of metadata publication for HTTP/GET requests.
Public propertyHttpsGetBindingGets or sets a binding used to configure metadata retrieval when the transport is HTTPS.
Public propertyHttpsGetEnabledGets or sets a value that indicates whether to publish service metadata for retrieval using an HTTPS/GET request.
Public propertyHttpsGetUrlGets or sets the location of metadata publication for HTTPS/GET requests.
Public propertyMetadataExporterGets or sets the internal MetadataExporter object used to publish the service metadata.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public fieldStatic memberMexContractNameReturns the string IMetadataContract.
Top

  NameDescription
Explicit interface implemetationPrivate methodIServiceBehavior.AddBindingParametersImplementation of IServiceBehavior that configures the underlying bindings to support the behavior.
Explicit interface implemetationPrivate methodIServiceBehavior.ApplyDispatchBehaviorImplementation of IServiceBehavior that configures the underlying bindings to support the behavior on the service.
Explicit interface implemetationPrivate methodIServiceBehavior.ValidateImplementation of IServiceBehavior that validates that the service description can support the behavior.
Top

Add a ServiceMetadataBehavior object to the ServiceDescription.Behaviors collection (or the <serviceMetadata> element in an application configuration file) to enable or disable the publication of service metadata. However, adding the behavior to a service is not sufficient to enable metadata publication:

  • To enable WS-Transfer GET metadata retrieval, you must also add an endpoint to your service in which the contract is IMetadataExchange. For an example, see How to: Publish Metadata for a Service Using Code. The IMetadataExchange endpoint can be configured as can any other endpoint.

  • To enable HTTP GET metadata retrieval, set the HttpGetEnabled property to true. For more information about the address of HTTP GET metadata, see HttpGetEnabled.

The address of the IMetadataExchange endpoint follows the normal rules regarding the combination of base addresses and endpoint addresses. For more information, see Publishing Metadata.

To enable the publication of metadata using a configuration file, add the <serviceMetadata> element to the <serviceBehaviors> element and associate the element with the <service> element for which you want to publish metadata. For an example, see How to: Publish Metadata for a Service Using a Configuration File. The class has the following members:

  • The HttpGetEnabled property specifies whether metadata is returned for HTTP/GET requests.

  • The HttpGetUrl property (in conjunction with the base addresses) specifies the HTTP/GET address.

  • The HttpsGetEnabled property specifies whether metadata is returned for an HTTPS/GET request.

  • The HttpsGetUrl property (in conjunction with the base addresses) specifies the HTTPS/GET address.

  • The MetadataExporter property returns the underlying exporter.

Typically the ServiceMetadataBehavior is used from an application configuration file. See the Example section for a code example.

The following code example demonstrates the use of ServiceMetadataBehavior in a configuration file to enable metadata support for HTTP GET and WS-Transfer GET requests.

<configuration>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="metadataSupport"
      >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/ServiceMetadata" />
          </baseAddresses>
        </host>
        <endpoint
          address="/SampleService"
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
        />
        <!-- Adds a WS-MetadataExchange endpoint at -->
        <!-- "http://localhost:8080/ServiceMetadata/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/ServiceMetadata?wsdl" -->
          <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
        // Create a new metadata behavior object and set its properties to  
        // create a secure endpoint. 
        ServiceMetadataBehavior sb = new ServiceMetadataBehavior();
        //sb.EnableHelpPage= true; 
        //sb.HttpsGetUrl = new Uri("https://myMachineName:8036/myEndpoint");
        //myServiceHost.Description.Behaviors.Add(sb);


    }

      private void SnippetServiceMetadataBehavior()
      {
          // service for which <<indigo2>> automatically adds a  
          // ServiceMetadataBehavior to publish metadata as well as  
          // an HTML service help page 

          // from C_HowToSecureEndpoint\cs 
          // Create a new metadata behavior object and set its properties to  
          // create a secure endpoint. 
          ServiceMetadataBehavior sb = new ServiceMetadataBehavior();
/*          sb.EnableHelpPage = true;
          sb.enableMetadataExchange = true;
          sb.HttpsGetUrl = new Uri("https://myMachineName:8036/myEndpoint");
          myServiceHost.Description.Behaviors.Add(sb);
 */


      }

    private void Run()
    {

      // T:System.ServiceModel.ServiceMetadataBehavior 
      // <Snippet#0> 

      // Create a ServiceHost for the service type and use the base address from configuration.
      ServiceHost host = new ServiceHost(typeof(SampleService));
      try
      {
        ServiceMetadataBehavior metad 
          = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
        if (metad == null)
          metad = new ServiceMetadataBehavior();
        metad.HttpGetEnabled = true;
        host.Description.Behaviors.Add(metad);
        host.AddServiceEndpoint(
          ServiceMetadataBehavior.MexContractName, 
          MetadataExchangeBindings.CreateMexHttpBinding(), 
          "mex"
        );

        // 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();

        // </Snippet#0>
<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>
<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>

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft