How to: Use MetadataExchangeClient to Retrieve Metadata
Use the MetadataExchangeClient class to download metadata using the WS-MetadataExchange (MEX) protocol. The retrieved metadata files are returned as a MetadataSet object. The returned MetadataSet object contains a collection of MetadataSection objects, each of which contains a specific metadata dialect and an identifier. You can write the returned metadata to files or, if the returned metadata contains Web Services Description Language (WSDL) documents, you can import the metadata using the WsdlImporter.
The MetadataExchangeClient constructors that take an address use the binding on the MetadataExchangeBindings static class that matches the Uniform Resource Identifier (URI) scheme of the address. You can alternatively use the MetadataExchangeClient constructor that allows you to explicitly specify the binding to use. The specified binding is used to resolve all metadata references.
Just like any other Windows Communication Foundation (WCF) client, the MetadataExchangeClient type provides a constructor for loading client endpoint configurations using the endpoint configuration name. The specified endpoint configuration must specify the IMetadataExchange contract. The address in the endpoint configuration is not loaded, so you must use one of the GetMetadata overloads that take an address. When you specify the metadata address using an EndpointAddress instance, the MetadataExchangeClient assumes that the address points to a MEX endpoint. If you specify the metadata address as a URL, then you need to also specify which MetadataExchangeClientMode to use, MEX or HTTP GET.
By default, the MetadataExchangeClient resolves all references for you, including WSDL and XML Schema imports and includes. You can disable this functionality by setting the ResolveMetadataReferences property to
false. You can control the maximum number of references to resolve using the MaximumResolvedReferences property. You can use this property in conjunction with the
MaxReceivedMessageSize property on the binding to control how much metadata is retrieved.
To use MetadataExchangeClient to obtain metadata
Create a new MetadataExchangeClient object by explicitly specifying a binding, an endpoint configuration name, or the address of the metadata.
Configure the MetadataExchangeClient to suit your needs. For example, you can specify credentials to use when requesting metadata, control how metadata references are resolved, and set the OperationTimeout property to control how long the metadata request has to return before it times out.
Obtain the MetadataSet object that contains the retrieved metadata by calling one of the GetMetadata methods. Note that you can only use the GetMetadata overload that takes no arguments if you explicitly specified an address when constructing the MetadataExchangeClient.
The following code example shows how to use MetadataExchangeClient to download and enumerate metadata files.
// Get metadata documents. Console.WriteLine("URI of the metadata documents retreived:"); MetadataExchangeClient metaTransfer = new MetadataExchangeClient(httpGetMetaAddress.Uri, MetadataExchangeClientMode.HttpGet); metaTransfer.ResolveMetadataReferences = true; MetadataSet otherDocs = metaTransfer.GetMetadata(); foreach (MetadataSection doc in otherDocs.MetadataSections) Console.WriteLine(doc.Dialect + " : " + doc.Identifier);
Compiling the Code
To compile this code example, you must reference the System.ServiceModel.dll assembly and import the System.ServiceModel.Description namespace.