Rediger

Del via


How to: Import Custom WSDL

This topic describes how to import custom WSDL. To handle the custom WSDL, you must implement the IWsdlImportExtension interface.

To import custom WSDL

  1. Implement IWsdlImportExtension. Implement the BeforeImport(ServiceDescriptionCollection, XmlSchemaSet, ICollection<XmlElement>) method to modify the metadata before it is imported. Implement the ImportEndpoint(WsdlImporter, WsdlEndpointConversionContext) and ImportContract(WsdlImporter, WsdlContractConversionContext) methods to modify contracts and endpoints imported from the metadata. To access the imported contract or endpoint, use the corresponding context object (WsdlContractConversionContext or WsdlEndpointConversionContext):

    public class WsdlDocumentationImporter : IWsdlImportExtension
    {
        public void ImportContract(WsdlImporter importer, WsdlContractConversionContext context)
        {
            // Contract documentation
            if (context.WsdlPortType.Documentation != null)
            {
                context.Contract.Behaviors.Add(new WsdlDocumentationImporter(context.WsdlPortType.Documentation));
            }
            // Operation documentation
            foreach (Operation operation in context.WsdlPortType.Operations)
            {
                if (operation.Documentation != null)
                {
                    OperationDescription operationDescription = context.Contract.Operations.Find(operation.Name);
                    if (operationDescription != null)
                    {
                        operationDescription.Behaviors.Add(new WsdlDocumentationImporter(operation.Documentation));
                    }
                }
            }
        }
    
        public void BeforeImport(ServiceDescriptionCollection wsdlDocuments, XmlSchemaSet xmlSchemas, ICollection<XmlElement> policy)
        {
            Console.WriteLine("BeforeImport called.");
        }
    
        public void ImportEndpoint(WsdlImporter importer, WsdlEndpointConversionContext context)
        {
            Console.WriteLine("ImportEndpoint called.");
        }
    }
    
  2. Configure the client application to use the custom WSDL importer. Note that if you are using Svcutil.exe, you should add this configuration to the configuration file for Svcutil.exe (Svcutil.exe.config):

    <system.serviceModel>  
          <client>  
            <endpoint
              address="http://localhost:8000/Fibonacci"
              binding="wsHttpBinding"  
              contract="IFibonacci"  
            />  
            <metadata>  
              <wsdlImporters>  
                <extension type="Microsoft.WCF.Documentation.WsdlDocumentationImporter, WsdlDocumentation" />  
              </wsdlImporters>  
            </metadata>  
          </client>  
        </system.serviceModel>  
    
  3. Create a new WsdlImporter instance (passing in the MetadataSet instance that contains the WSDL documents that you want to import), and call ImportAllContracts:

    WsdlImporter importer = new WsdlImporter(metaDocs);
    System.Collections.ObjectModel.Collection<ContractDescription> contracts = importer.ImportAllContracts();  
    

See also