Export (0) Print
Expand All

How to: Export Metadata from Service Endpoints

This topic explains how to export metadata from service endpoints.

To export metadata from service endpoints

  1. Create a new Visual Studio Console App Project. Add the code shown in the following steps in the generated Program.cs file within the main() method.

  2. Create a WsdlExporter.

    WsdlExporter exporter = new WsdlExporter();
    
    
  3. Set the PolicyVersion property to one of the values from the PolicyVersion enumeration. This sample sets the value to Policy15 which corresponds to WS-Policy 1.5.

    exporter.PolicyVersion = PolicyVersion.Policy15;
    
    
  4. Create an array of ServiceEndpoint objects.

    ServiceEndpoint [] myServiceEndpoints = new ServiceEndpoint[2];
    ContractDescription myDescription = new ContractDescription ("myContract");
    myServiceEndpoints[0] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("http://localhost/myservice"));
    myServiceEndpoints[1] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("http://localhost/myservice"));
    
    
  5. Export metadata for each service endpoint.

    // Export all endpoints for each endpoint in collection.
    foreach (ServiceEndpoint endpoint in myServiceEndpoints)
    {
        exporter.ExportEndpoint(endpoint);
    }
    
    
  6. Check to make sure no errors occurred during the export process and retrieve the metadata.

    // If there are no errors, get the documents.
    MetadataSet metadataDocs = null;
    if (exporter.Errors.Count != 0)
    {
        metadataDocs = exporter.GetGeneratedMetadata();
    }
    
    
  7. You can now use the metadata, such as write it to a file by calling the WriteTo method.

Example

The following is the full code listing for this example.

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WsdlExporterSample
{
    class Program
    {
        static void Main(string[] args)
        {
            WsdlExporter exporter = new WsdlExporter();
            exporter.PolicyVersion = PolicyVersion.Policy15;
          
            ServiceEndpoint [] myServiceEndpoints = new ServiceEndpoint[2];
            ContractDescription myDescription = new ContractDescription ("myContract");
            myServiceEndpoints[0] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("http://localhost/myservice"));
            myServiceEndpoints[1] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("http://localhost/myservice"));
            
            // Export all endpoints for each endpoint in collection.
            foreach (ServiceEndpoint endpoint in myServiceEndpoints)
            {
                exporter.ExportEndpoint(endpoint);
            }
            // If there are no errors, get the documents.
            MetadataSet metadataDocs = null;
            if (exporter.Errors.Count != 0)
            {
                metadataDocs = exporter.GetGeneratedMetadata();
            }
        }
    }
}

Compiling the Code

When compiling Program.cs reference System.ServiceModel.dll.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft