This documentation is archived and is not being maintained.

WsdlImporter Class

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


Namespace:  System.ServiceModel.Description
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public class WsdlImporter : MetadataImporter

The WsdlImporter type exposes the following members.

Public methodWsdlImporter(MetadataSet)Initializes a new instance of the WsdlImporter class.
Public methodWsdlImporter(MetadataSet, IEnumerable<IPolicyImportExtension>, IEnumerable<IWsdlImportExtension>)Creates a WsdlImporter object from the specified metadata, custom policy importers, and custom WSDL importers.
Public methodWsdlImporter(MetadataSet, IEnumerable<IPolicyImportExtension>, IEnumerable<IWsdlImportExtension>, MetadataImporterQuotas)Creates a WsdlImporter object from the specified metadata, custom policy importers, and custom WSDL importers.

Public propertyErrorsGets a value that indicates whether there were errors importing the metadata. (Inherited from MetadataImporter.)
Public propertyKnownContractsGets a dictionary of contracts by name that the importer knows about. (Inherited from MetadataImporter.)
Public propertyPolicyImportExtensionsGets a collection of policy importers that the importer calls to process policy assertions. (Inherited from MetadataImporter.)
Public propertyStateGets or sets a collection of objects used in the importing of metadata. (Inherited from MetadataImporter.)
Public propertyWsdlDocumentsGets a set of ServiceDescription objects that describe the contract information in the metadata documents.
Public propertyWsdlImportExtensionsGets a set of IWsdlImportExtension objects used to import custom WSDL information.
Public propertyXmlSchemasGets a set of XmlSchema objects that describe types in the metadata.

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodImportAllBindingsReturns a set of Binding objects imported from the metadata documents.
Public methodImportAllContractsReturns a set of ContractDescription objects that represent port type information in the metadata documents. (Overrides MetadataImporter.ImportAllContracts().)
Public methodImportAllEndpointsReturns a ServiceEndpointCollection that represents the endpoints in WSDL documents. (Overrides MetadataImporter.ImportAllEndpoints().)
Public methodImportBindingReturns a Binding object that represents binding information from a set of metadata documents.
Public methodImportContractReturns a ContractDescription object that represents metadata located by the specified port type information.
Public methodImportEndpointReturns a ServiceEndpoint from a set of metadata documents that uses information from the specified System.Web.Services.Description.Port object.
Public methodImportEndpoints(Binding)Returns a ServiceEndpointCollection that represents all WSDL port types using the specified System.Web.Services.Description.Binding.
Public methodImportEndpoints(PortType)Returns a ServiceEndpointCollection that represents all WSDL port types associated with the specified System.Web.Services.Description.PortType.
Public methodImportEndpoints(Service)Returns a ServiceEndpointCollection that represents all WSDL port types within the specified System.Web.Services.Description.Service.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

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.

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

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.