方法: メタデータをサービス エンドポイントからエクスポートする

このトピックでは、メタデータをサービス エンドポイントからエクスポートする方法について説明します。

メタデータをサービス エンドポイントからエクスポートするには

  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 への参照を追加してください。

関連項目