How to: Export Metadata from Service Endpoints
This topic explains how to export metadata from service endpoints.
To export metadata from service endpoints
-
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.
-
Create a WsdlExporter.
-
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.
-
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"));
-
Export metadata for each service endpoint.
-
Check to make sure no errors occurred during the export process and retrieve the metadata.
-
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.