1. Why do we need to get the metadata on client side?
Sometimes the client need to programmatically verify whether a participant endpoint (identified by its address) supports a participant contract/operations. for example, imagine an application where the End User specifies or configures the application during setup to consume and interact with a service. if the service does not support the required contracts, the application shocould alert the user that an Invalid Address was specified, and ask for an alternative or a corretct address.
In order to support such functinoality, the application needs to retrieve the metadata OS the serve endpoints and see if at least one of the endpoints supports the requetsted contract.
As we know, when contracts of a serviced are exposed, we can consume the service through endpoint. Actually, there are two kinds of endpoint with the contracts exposed -- Bisiness endpoint And Mex endpoint. And Mex endpoint is the interface from which the client side can retrieve the metadata, such as what contracts are exposed. etc, just as the dimo-shows.
Sometimes, when a client consumes a specific service, the client needsProgram Check whether a specific Endpoint specified by an address supports a contract. For example, in an application, the end user must interact with the service to set the application. If a service does not support the corresponding contract, inform the user so that the user can make another choice or select the correct service address.
So how do I know the contracts/operation supported by the published wcfservice? We need to obtain the metadata of the service on the client.
We know that the WCF Service publishes the service through contract, and then we use endpoint to consume the service. In fact, every time a service is published, we get two types of Endpoints: one is the service endpoint (the endpoint we call generally refers to) and the other is the metadata endpoint.
// Note: Metadata is commonly used to describe data. This metadata describes the endpoint, contract, and operaion of the published service.
2. How to retrieve metadata?
There are several classes concerned with retrieving metadata on client side. They are:
I) Public Enum metadataexchangeclientmode
{
Metadataexchange,
Httpget
}
This enumeration specify the metadata exchange mode
This enumeration type defines the metadata exchange mode.
Ii) Class metadataset
This class is designed to store the metadata as XML format, and the instance of this class can be a contructor parametor for class wsdlimport
This class is used to save the obtained metadata in XML format. The object of this class can be used as a parameter of the wsdlimporter constructor.
Iii) Class metadataexchangeclient
This class is very critical, for retrieving metadata from the client side. And its command method is getmetadata (), and its return value is a metadataset reference.
This class is the most critical and used to obtain metadata on the client. The commonly used method is getmetadata (), which returns a metadataset reference.
Iv) Public abstact class metadataimporter
This is an abstract class, is the base class of wsdlimporter.
Abstract class, used as the base class of wsdlimporter.
V) public class wsdlimport: metadataimporter
This class is used to process the metadataset, and generate respective collections, the comman methods is importallendpoints (), importallcontracts (). etc.
This class is used to process the obtained metadataset and obtain the collection of the corresponding metadata. Common methods include importallendpoints and importallcontracts.
3 The demo sample for retrieving metadata on the client side.
Using system. servicemodel;
Using system. servicemodel. description;
String mxaddress = http: // localhost: 8081/myservice? WSDL;
// This is the service address exposed on the server side.
Metadataexchangeclient mxclient = new metadataexchangeclient (New uri (mxaddress), metadataexchangeclientmode. httpget );
// Instantiate an instance mxclient
Metadataset metadta = mxclient. getmetadata ();
// Retrieve the metadata, stored as metadataset
Metadataimporter importer = new wsdlimporter (metadta );
Serviceendpointcollection endpoints = importer. importallendpoints ();
// Get relavant collection from metadataset.
Foreach (serviceendpoint endpoint in endpoints)
Console. writeline ("Address: {0}, contract name: {1}, namespace: {2}", endpoint. address. tostring (), endpoint. contract. name, endpoint. contract. namespace );
// Show the results by the console.