Export (0) Print
Expand All

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

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.

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();
		}

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

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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0

Community Additions

ADD
Show:
© 2014 Microsoft