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)

System.Object
  System.ServiceModel.Description.MetadataImporter
    System.ServiceModel.Description.WsdlImporter

public class WsdlImporter : MetadataImporter

NameDescription
System_CAPS_pubmethodWsdlImporter(MetadataSet)

Initializes a new instance of the WsdlImporter class.

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

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

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

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

NameDescription
System_CAPS_pubpropertyErrors

Gets a value that indicates whether there were errors importing the metadata.(Inherited from MetadataImporter.)

System_CAPS_pubpropertyKnownContracts

Gets a dictionary of contracts by name that the importer knows about.(Inherited from MetadataImporter.)

System_CAPS_pubpropertyPolicyImportExtensions

Gets a collection of policy importers that the importer calls to process policy assertions.(Inherited from MetadataImporter.)

System_CAPS_pubpropertyState

Gets or sets a collection of objects used in the importing of metadata.(Inherited from MetadataImporter.)

System_CAPS_pubpropertyWsdlDocuments

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

System_CAPS_pubpropertyWsdlImportExtensions

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

System_CAPS_pubpropertyXmlSchemas

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

NameDescription
System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodImportAllBindings()

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

System_CAPS_pubmethodImportAllContracts()

Returns a set of ContractDescription objects that represent port type information in the metadata documents.(Overrides MetadataImporter.ImportAllContracts().)

System_CAPS_pubmethodImportAllEndpoints()

Returns a ServiceEndpointCollection that represents the endpoints in WSDL documents.(Overrides MetadataImporter.ImportAllEndpoints().)

System_CAPS_pubmethodImportBinding(Binding)

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

System_CAPS_pubmethodImportContract(PortType)

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

System_CAPS_pubmethodImportEndpoint(Port)

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

System_CAPS_pubmethodImportEndpoints(Binding)

Returns a ServiceEndpointCollection that represents all WSDL port types using the specified System.Web.Services.Description.Binding.

System_CAPS_pubmethodImportEndpoints(PortType)

Returns a ServiceEndpointCollection that represents all WSDL port types associated with the specified System.Web.Services.Description.PortType.

System_CAPS_pubmethodImportEndpoints(Service)

Returns a ServiceEndpointCollection that represents all WSDL port types within the specified System.Web.Services.Description.Service.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns 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.

System_CAPS_noteNote

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);
    }
    else
    {
      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(ext);
    }
    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();
    importer.ImportAllEndpoints();
	foreach (ContractDescription contract in contracts)
	{
		generator.GenerateServiceContractType(contract);
	}
    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));
	codeDomProvider.GenerateCodeFromCompileUnit(
      generator.TargetCompileUnit, textWriter, options
    );
	textWriter.Close();
}

.NET Framework
Available since 3.0

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

Return to top
Show: