如何:从服务终结点导出元数据

本主题介绍如何从服务终结点导出元数据。

从服务终结点导出元数据

  1. 创建一个新的 Visual Studio 控制台应用程序项目。 在生成的 Program.cs 文件的 main() 方法中添加下列步骤所示的代码。

  2. 创建 WsdlExporter

    WsdlExporter exporter = new WsdlExporter();
    
    Dim exporter As New WsdlExporter()
    
  3. PolicyVersion 属性设置为 PolicyVersion 枚举值之一。 此示例将该值设置为与 WS-Policy 1.5 对应的 Policy15

    exporter.PolicyVersion = PolicyVersion.Policy15;
    
    exporter.PolicyVersion = PolicyVersion.Policy15
    
  4. 创建 ServiceEndpoint 对象的数组。

    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"));
    
    Dim myServiceEndpoints() As ServiceEndpoint = New ServiceEndpoint(1) {}
    Dim myDescription As 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 all endpoints for each endpoint in collection.
    foreach (ServiceEndpoint endpoint in myServiceEndpoints)
    {
        exporter.ExportEndpoint(endpoint);
    }
    
    'Export all endpoints for each endpoint in collection.
    For Each endpoint As ServiceEndpoint In myServiceEndpoints
        exporter.ExportEndpoint(endpoint)
    Next
    
  6. 检查以确保在导出过程中不会发生任何错误,并检索元数据。

    // If there are no errors, get the documents.
    MetadataSet metadataDocs = null;
    if (exporter.Errors.Count != 0)
    {
        metadataDocs = exporter.GetGeneratedMetadata();
    }
    
    'If there are no errors, get the documents.
    Dim metadataDocs As MetadataSet
    metadataDocs = Nothing
    
    If (exporter.Errors.Count = 0) Then
        metadataDocs = exporter.GetGeneratedMetadata()
    End If
    
  7. 现在可以使用元数据,例如通过调用 WriteTo(XmlWriter) 方法将它写入文件。

示例

下面列出了此示例的完整代码。

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();
            }
        }
    }
}
Imports System.ServiceModel
Imports System.ServiceModel.Description

Module Module1

    Sub Main()
        Dim exporter As New WsdlExporter()
        exporter.PolicyVersion = PolicyVersion.Policy15

        Dim myServiceEndpoints() As ServiceEndpoint = New ServiceEndpoint(1) {}
        Dim myDescription As 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.
        For Each endpoint As ServiceEndpoint In myServiceEndpoints
            exporter.ExportEndpoint(endpoint)
        Next

        'If there are no errors, get the documents.
        Dim metadataDocs As MetadataSet
        metadataDocs = Nothing

        If (exporter.Errors.Count = 0) Then
            metadataDocs = exporter.GetGeneratedMetadata()
        End If
    End Sub

End Module

编译代码

编译 Program.cs 时引用 System.ServiceModel.dll。

另请参阅