Share via


How to: Import Metadata into Service Endpoints

This topic explains how to import metadata into a collection of service endpoints and use the service defined in the Getting Started Sample. This topic show how to create a client application that imports metadata from the service and then calls the Add method on the service.

To import metadata into service endpoints

  1. Declare an EndpointAddress object and initialize it with the Uniform Resource Identifier (URI) for the metadata exchange (MEX) address of the service.

    EndpointAddress mexAddress = new EndpointAddress("https://localhost:8000/ServiceModelSamples/service/mex");
    
  2. Create a MetadataExchangeClient, passing in the MEX address, and call GetMetadata. This retrieves the metadata from the service.

    MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress);
    mexClient.ResolveMetadataReferences = true;
    MetadataSet metaSet = mexClient.GetMetadata();
    
  3. Create a WsdlImporter, passing in the metadata previously retrieved, and call ImportAllContracts. This generates a collection of ContractDescription objects. You could also call ImportAllEndpoints or ImportAllBindings, depending upon your needs.

    WsdlImporter importer = new WsdlImporter(metaSet);
    System.Collections.ObjectModel.Collection<ContractDescription> contracts = importer.ImportAllContracts();
    
    ms733780.note(en-us,VS.85).gifNote:
    After you have imported the metadata, you will not be able to create a client channel or export the metadata. This is because no type information is available at this point. Type information is required to actually interact with the service or export metadata. To generate the type information, you need to generate code, shown in steps 4 and 5. Alternatively, you could use the MetadataResolver helper class. For more information, see How to: Use MetadataResolver to Obtain Binding Metadata Dynamically.

  4. Generate type information for each contract.

    ServiceContractGenerator generator = new ServiceContractGenerator();
    foreach (ContractDescription contract in contracts)
    {
        generator.GenerateServiceContractType(contract);
    }
    
    if (generator.Errors.Count != 0)
        throw new Exception("There were errors during code compilation.");
    
  5. Now you can use this information. The following sample generates C# source code.

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

See Also

Concepts

Metadata in Windows Communication Foundation

Other Resources

Getting Started Sample


© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07