方法 : カスタム WS-Metadata Exchange バインディングを構成する

ここでは、カスタム WS-Metadata Exchange バインディングを構成する方法について説明します。Windows Communication Foundation (WCF) には、4 つのシステム定義のメタデータ バインディングがありますが、どのバインディングでもメタデータを公開できます。ここでは、wsHttpBinding を使用してメタデータを公開する方法を示します。このバインディングでは、メタデータをセキュリティで保護して公開することができます。ここに示すコードは、「Getting Started Sample」に基づいています。

構成ファイルの使用

  1. サービスの構成ファイルで、serviceMetadata タグを含んだサービス動作を追加します。

    <behaviors>
       <serviceBehaviors>
         <behavior name="CalculatorServiceBehavior">
           <serviceMetadata httpGetEnabled="True"/>
         </behavior>
       </serviceBehaviors>
    </behaviors>
    
  2. この新しい動作を参照する behaviorConfiguration 属性をサービス タグに追加します。

    <service        name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior"> 
    
  3. メタデータ エンドポイントを追加し、アドレスに mex、バインディングに wsHttpBinding、コントラクトに IMetadataExchange をそれぞれ指定します。

    <endpoint address="mex"
              binding="wsHttpBinding"
              contract="IMetadataExchange" />
    
  4. Metadata Exchange エンドポイントが適切に動作することを確認するには、クライアントの構成ファイルにエンドポイント タグを追加します。

    <endpoint name="MyMexEndpoint"               address="https://localhost:8000/servicemodelsamples/service/mex"
              binding="wsHttpBinding"
              contract="IMetadataExchange"/>
    
  5. クライアントの Main() メソッドで、新しい MetadataExchangeClient インスタンスを作成し、その ResolveMetadataReferences プロパティを true に設定し、GetMetadata を呼び出して返されるメタデータのコレクションを反復処理します。

    string mexAddress = "https://localhost:8000/servicemodelsamples/service/mex";
    
    MetadataExchangeClient mexClient = new MetadataExchangeClient("MyMexEndpoint");
    mexClient.ResolveMetadataReferences = true;
    MetadataSet mdSet = mexClient.GetMetadata(new EndpointAddress(mexAddress));
    foreach (MetadataSection section in mdSet.MetadataSections)
    Console.WriteLine("Metadata section: " + section.Dialect.ToString());
    

コードによる構成

  1. WsHttpBinding バインディングのインスタンスを作成します。

    WSHttpBinding binding = new WSHttpBinding();
    
  2. ServiceHost インスタンスを作成します。

    ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
    
  3. サービス エンドポイントと ServiceMetadataBehavior インスタンスを追加します。

    serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, baseAddress);
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    serviceHost.Description.Behaviors.Add(smb);
    
  4. 前に作成した WSHttpBinding を指定する Metadata Exchange エンドポイントを追加します。

    serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, mexAddress);
    
  5. Metadata Exchange エンドポイントが適切に動作することを確認するには、クライアントの構成ファイルにエンドポイント タグを追加します。

    <endpoint name="MyMexEndpoint"               address="https://localhost:8000/servicemodelsamples/service/mex"
              binding="wsHttpBinding"
              contract="IMetadataExchange"/>
    
  6. クライアントの Main() メソッドで、新しい MetadataExchangeClient インスタンスを作成し、その ResolveMetadataReferences プロパティを true に設定し、GetMetadata を呼び出して返されるメタデータのコレクションを反復処理します。

    string mexAddress = "https://localhost:8000/servicemodelsamples/service/mex";
    
    MetadataExchangeClient mexClient = new MetadataExchangeClient("MyMexEndpoint");
    mexClient.ResolveMetadataReferences = true;
    MetadataSet mdSet = mexClient.GetMetadata(new EndpointAddress(mexAddress));
    foreach (MetadataSection section in mdSet.MetadataSections)
    Console.WriteLine("Metadata section: " + section.Dialect.ToString());
    

関連項目

概念

メタデータ
メタデータの公開
メタデータ エンドポイントを公開する

その他の技術情報

Metadata Publishing Behavior
Retrieve Metadata