MSDN Note:
Windows Communication Foundation (WCF) services publish metadata by publishing one or more metadata endpoints. Publishing service metadata makes the metadata available using standardized protocols, such as WS-MetadataExchange (MEX) and HTTP/GET requests. Metadata endpoints are similar to other service endpoints in that they have an address, a binding, and a contract, and they can be added to a service host through configuration or imperative code.
To publish metadata endpoints for a WCF service, you first must add the ServiceMetadataBehavior service behavior to the service. Adding a System.ServiceModel.Description.ServiceMetadataBehavior instance allows your service to expose metadata endpoints. Once you add the System.ServiceModel.Description.ServiceMetadataBehavior service behavior, you can then expose metadata endpoints that support the MEX protocol or that respond to HTTP/GET requests.
The System.ServiceModel.Description.ServiceMetadataBehavior uses a WsdlExporter to export metadata for all service endpoints in your service. For more information about exporting metadata from a service, see Exporting and Importing Metadata.
The System.ServiceModel.Description.ServiceMetadataBehavior adds a ServiceMetadataExtension instance as an extension to your service host. The System.ServiceModel.Description.ServiceMetadataExtension provides the implementation for the metadata publishing protocols. You can also use theSystem.ServiceModel.Description.ServiceMetadataExtension to get the service's metadata at runtime by accessing theSystem.ServiceModel.Description.ServiceMetadataExtension.Metadata property.
To add metadata endpoints that use the MEX protocol, add service endpoints to your service host that use theIMetadataExchange service contract. WCF includes an IMetadataExchange interface with this service contract name that you can use as part of the WCF programming model. WS-MetadataExchange endpoints, or MEX endpoints, can use one of the four default bindings that the static factory methods expose on the MetadataExchangeBindings class to match the default bindings used by WCF tools such as Svcutil.exe. You can also configure MEX metadata endpoints using your own custom binding.
NOTE:
The Metadata Exchange Endpoint (MEX) is a special endpoint in WCF that exposes metadata used to describe a service.
the code looks like blow.
or the config looks like below.
and the code with the config looks like :
HTTP GET Metadata Endpoints
To add a metadata endpoint to your service that responds to HTTP/GET requests, set the HttpGetEnabled property on the System.ServiceModel.Description.ServiceMetadataBehavior to true. You can also configure a metadata endpoint that uses HTTPS by setting the HttpsGetEnabled property on the System.ServiceModel.Description.ServiceMetadataBehaviorto true.
WCF services publish metadata by exposing one or more metadata endpoints. Publishing service metadata makes service metadata available using standardized protocols, such as MEX and HTTP/GET requests. Metadata endpoints are similar to other service endpoints in that they have an address, a binding, and a contract. You can add metadata endpoints to a service host in configuration or in code.
To publish metadata endpoints for a WCF service, you must first add an instance of the ServiceMetadataBehavior service behavior to the service. (otherwise when creating the mex endpoint. you will got a error message shows blow in light blue fonts. )Adding a System.ServiceModel.Description.ServiceMetadataBehavior instance to your service augments your service with the ability to publish metadata by exposing one or more metadata endpoints. Once you add the System.ServiceModel.Description.ServiceMetadataBehavior service behavior you can then expose metadata endpoints that support the MEX protocol or metadata endpoints that respond to HTTP/GET requests.
To add metadata endpoints that use the MEX protocol, add service endpoints to your service host that use the service contract named IMetadataExchange.WCF defines the IMetadataExchange interface that has this service contract name. WS-MetadataExchange endpoints, or MEX endpoints, can use one of the four default bindings exposed by the static factory methods on the MetadataExchangeBindings class to match the default bindings used by WCF tools, such as Svcutil.exe. You can also configure MEX metadata endpoints using a custom binding.
The ServiceMetadataBehavior uses a System.ServiceModel.Description.WsdlExporter to export metadata for all service endpoints in your service. For more information about exporting metadata from a service, see Exporting and Importing Metadata.
The ServiceMetadataBehavior augments your service host by adding a ServiceMetadataExtension instance as an extension to your service host. The System.ServiceModel.Description.ServiceMetadataExtension provides the implementation for the metadata publishing protocols. You can also use the System.ServiceModel.Description.ServiceMetadataExtension to get the service's metadata at runtime by accessing the Metadata property.
If you add a MEX endpoint in your application configuration file and then attempt to add the ServiceMetadataBehaviorto your service host in code you get the following exception: System.InvalidOperationException: The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service Service1. Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract. You can work around this issue by either adding the ServiceMetadataBehavior in the configuration file or adding both the endpoint and ServiceMetadataBehavior in code. For an example of adding ServiceMetadataBehavior in an application configuration file, see the Getting Started Sample. For an example of adding ServiceMetadataBehavior in code, see the Self-Host sample.
|
When publishing metadata for a service that exposes two different service contracts in which each contain an operation of the same name an exception is thrown. For example, if you have a service that exposes a service contract called ICarService that has an operation Get(Car c) and the same service exposes a service contract called IBookService that has an operation Get(Book b), an exception is thrown or an error message is displayed when generating the service's metadata. To work around this issue do one of the following:
|
see also
http://msdn.microsoft.com/en-us/library/ms734765.aspx