Exporting and Importing Metadata
In Windows Communication Foundation (WCF), exporting metadata is the process of describing service endpoints and projecting them into a parallel, standardized representation that clients can use to understand how to use the service. Importing service metadata is the process of generating ServiceEndpoint instances or parts from service metadata.
To export metadata from System.ServiceModel.Description.ServiceEndpoint instances, use an implementation of the MetadataExporter abstract class. The WsdlExporter type is the implementation of the MetadataExporter abstract class included with WCF.
The System.ServiceModel.Description.WsdlExporter type generates Web Services Description Language (WSDL) metadata with attached policy expressions encapsulated in a MetadataSet instance. You can use a System.ServiceModel.Description.WsdlExporter instance to iteratively export metadata for ContractDescription objects and ServiceEndpoint objects. You can also export a collection of ServiceEndpoint objects and associate them with a specific service name.
You can only use the WsdlExporter to export metadata from ContractDescription instances that contain common language runtime (CLR) type information, such as a ContractDescription instance created using the ContractDescription.GetContract method or created as part of the ServiceDescription for a ServiceHost instance. You cannot use the WsdlExporter to export metadata from ContractDescription instances imported from service metadata or constructed without type information.
Importing WSDL Documents
To import service metadata in WCF, use an implementation of the MetadataImporter abstract class. The System.ServiceModel.Description.WsdlImporter type is the implementation of the MetadataImporter abstract class included with WCF. The WsdlImporter type imports WSDL metadata with attached policies bundled in a MetadataSet object.
The WsdlImporter type gives you control over how to import the metadata. You can import all of the endpoints, all of the bindings, or all of the contracts. You can import all of the endpoints associated with a specific WSDL service, binding, or port type. You can also import the endpoint for a specific WSDL port, the binding for a specific WSDL binding or the contract for a specific WSDL port type.
The WsdlImporter also exposes a KnownContracts property that allows you to specify a set of contracts that do not need to be imported. The WsdlImporter uses the contracts in the KnownContracts property instead of importing a contract with the same qualified name from the metadata.
The WsdlImporter type collects the policy expressions attached to the message, operation, and endpoint policy subjects and then uses the IPolicyImportExtension implementations in the PolicyImportExtensions collection to import the policy expressions.
The policy import logic automatically handles policy references to policy expressions in the same WSDL document and is identified with a wsu:Id or xml:id attribute. The policy import logic protects applications against circular policy references by limiting the size of a policy expression to 4096 nodes, where a node is a one of the following elements: wsp:Policy, wsp:All, wsp:ExactlyOne, wsp:policyReference.
The policy import logic also automatically normalizes policy expressions. Nested policy expressions and the wsp:Optional attribute are not normalized. The amount of normalization processing done is limited to 4096 steps, where each step yields a policy assertion, or a child element of a wsp:ExactlyOne element.
The WsdlImporter type tries up to 32 combinations of policy alternatives attached to the different WSDL policy subjects. If no combination imports cleanly, the first combination is used to construct a partial custom binding.
Both the MetadataExporter and the MetadataImporter types expose an Errors property that can contain a collection of error and warning messages encountered during the export and import processes, respectively, that can be used when implementing tools.
The WsdlImporter type generally throws an exception for an exception caught during the import process and adds a corresponding error to its Errors property. The ImportAllContracts, ImportAllBindings, ImportAllEndpoints, and ImportEndpoints methods, however, do not throw these exceptions, so you must check the Errors property to determine if any issues occurred when calling these methods.
The WsdlExporter type rethrows any exceptions caught during the export process. These exceptions are not captured as errors in the Errors property. Once the WsdlExporter throws an exception, it is in a faulted state and cannot be reused. The WsdlExporter does add warnings to its Errors property when an operation cannot be exported because it uses wildcard actions and when duplicate binding names are encountered.