Freigeben über

WsdlImporter Class


Imports Web Services Description Language (WSDL) 1.1 metadata with WS-Policy attachments.

public ref class WsdlImporter : System::ServiceModel::Description::MetadataImporter
public class WsdlImporter : System.ServiceModel.Description.MetadataImporter
type WsdlImporter = class
    inherit MetadataImporter
Public Class WsdlImporter
Inherits MetadataImporter


The following code example shows how to use the WsdlImporter to add a custom System.Runtime.Serialization.IDataContractSurrogate, import all contracts, and compile those contracts and save them to a file.

  static void GenerateCSCodeForService(EndpointAddress metadataAddress, string outputFile)
MetadataExchangeClient mexClient = new MetadataExchangeClient(metadataAddress);
mexClient.ResolveMetadataReferences = true;
MetadataSet metaDocs = mexClient.GetMetadata();

      WsdlImporter importer = new WsdlImporter(metaDocs);
ServiceContractGenerator generator = new ServiceContractGenerator();

// Add our custom DCAnnotationSurrogate
// to write XSD annotations into the comments.
object dataContractImporter;
XsdDataContractImporter xsdDCImporter;
if (!importer.State.TryGetValue(typeof(XsdDataContractImporter), out dataContractImporter))
  Console.WriteLine("Couldn't find the XsdDataContractImporter! Adding custom importer.");
  xsdDCImporter = new XsdDataContractImporter();
  xsdDCImporter.Options = new ImportOptions();
  importer.State.Add(typeof(XsdDataContractImporter), xsdDCImporter);
  xsdDCImporter = (XsdDataContractImporter)dataContractImporter;
  if (xsdDCImporter.Options == null)
    Console.WriteLine("There were no ImportOptions on the importer.");
    xsdDCImporter.Options = new ImportOptions();
xsdDCImporter.Options.DataContractSurrogate = new DCAnnotationSurrogate();

// Uncomment the following code if you are going to do your work programmatically rather than add
// the WsdlDocumentationImporters through a configuration file.
// The following code inserts a custom WsdlImporter without removing the other
// importers already in the collection.
System.Collections.Generic.IEnumerable<IWsdlImportExtension> exts = importer.WsdlImportExtensions;
System.Collections.Generic.List<IWsdlImportExtension> newExts
  = new System.Collections.Generic.List<IWsdlImportExtension>();
foreach (IWsdlImportExtension ext in exts)
  Console.WriteLine("Default WSDL import extensions: {0}", ext.GetType().Name);
newExts.Add(new WsdlDocumentationImporter());
System.Collections.Generic.IEnumerable<IPolicyImportExtension> polExts = importer.PolicyImportExtensions;
importer = new WsdlImporter(metaDocs, polExts, newExts);

System.Collections.ObjectModel.Collection<ContractDescription> contracts
  = importer.ImportAllContracts();
      foreach (ContractDescription contract in contracts)
if (generator.Errors.Count != 0)
  throw new Exception("There were errors during code compilation.");

// Write the code dom
System.CodeDom.Compiler.CodeGeneratorOptions options
  = new System.CodeDom.Compiler.CodeGeneratorOptions();
      options.BracingStyle = "C";
      System.CodeDom.Compiler.CodeDomProvider codeDomProvider
  = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("C#");
      System.CodeDom.Compiler.IndentedTextWriter textWriter
  = new System.CodeDom.Compiler.IndentedTextWriter(new System.IO.StreamWriter(outputFile));
  generator.TargetCompileUnit, textWriter, options


Use the WsdlImporter class to import metadata as well as convert that information into various classes that represent contract and endpoint information.

The base class for WsdlImporter, the MetadataImporter class, defines methods that selectively import contract and endpoint information and properties that expose any import errors and accept type information relevant to the import and conversion process. The WsdlImporter type uses the custom policy importers (IPolicyImportExtension implementations) from its parent type to handle custom policy statements and its own custom WSDL importers (IWsdlImportExtension implementations) to handle custom WSDL elements. For details, see Extending the Metadata System.

When importing policy from WSDL documents, the WsdlImporter type will try 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.

In addition to these methods and properties, WsdlImporter also implements methods that support importing binding information and properties that provide access to any policy documents, WSDL documents, WSDL extensions, and XML schema documents. For information about extending WsdlImporter to support custom WSDL elements, see IWsdlImportExtension.

Typically the WsdlImporter class is used in a three-step process.

  1. Create a WsdlImporter object and pass a MetadataSet object to the constructor.

  2. Call the appropriate Import method to retrieve the results.

  3. Check the Errors property to determine whether there are any import errors.


When importing WSDL port types, if the QName of the port type matches an entry in the KnownContracts dictionary then the port type is not imported and the known contract is used instead.

No values are returned from the WsdlImporter properties until one of the import methods is called. Custom System.ServiceModel.Description.IWsdlImportExtension objects can either be loaded into the WsdlImporter programmatically or using the client configuration <wsdlImporters> element.

Metadata that has been imported as service endpoints cannot be used to create a runtime or export metadata because the imported endpoints contain no managed type information. To use the metadata to create a client or service runtime or to generate metadata, you must first generate and compile code from the metadata and use that type information to create a new System.ServiceModel.Description.ContractDescription object using GetContract.



Initializes a new instance of the WsdlImporter class.

WsdlImporter(MetadataSet, IEnumerable<IPolicyImportExtension>, IEnumerable<IWsdlImportExtension>)

Creates a WsdlImporter object from the specified metadata, custom policy importers, and custom WSDL importers.

WsdlImporter(MetadataSet, IEnumerable<IPolicyImportExtension>, IEnumerable<IWsdlImportExtension>, MetadataImporterQuotas)

Creates a WsdlImporter object from the specified metadata, custom policy importers, and custom WSDL importers.



Gets a value that indicates whether there were errors importing the metadata.

(Inherited from MetadataImporter)

Gets a dictionary of contracts by name that the importer knows about.

(Inherited from MetadataImporter)

Gets a collection of policy importers that the importer calls to process policy assertions.

(Inherited from MetadataImporter)

Gets or sets a collection of objects used in the importing of metadata.

(Inherited from MetadataImporter)

Gets a set of ServiceDescription objects that describe the contract information in the metadata documents.


Gets a set of IWsdlImportExtension objects used to import custom WSDL information.


Gets a set of XmlSchema objects that describe types in the metadata.



Determines whether the specified object is equal to the current object.

(Inherited from Object)

Serves as the default hash function.

(Inherited from Object)

Gets the Type of the current instance.

(Inherited from Object)

Returns a set of Binding objects imported from the metadata documents.


Returns a set of ContractDescription objects that represent port type information in the metadata documents.


Returns a ServiceEndpointCollection that represents the endpoints in WSDL documents.


Returns a Binding object that represents binding information from a set of metadata documents.


Returns a ContractDescription object that represents metadata located by the specified port type information.


Returns a ServiceEndpoint from a set of metadata documents that uses information from the specified Port object.


Returns a ServiceEndpointCollection that represents all WSDL port types using the specified Binding.


Returns a ServiceEndpointCollection that represents all WSDL port types associated with the specified PortType.


Returns a ServiceEndpointCollection that represents all WSDL port types within the specified Service.


Creates a shallow copy of the current Object.

(Inherited from Object)

Returns a string that represents the current object.

(Inherited from Object)

Applies to