方法 : カスタム バインディングを介してメタデータを取得する

ここでは、カスタム バインディングを介して MEX エンドポイントからメタデータを取得する方法を説明します。このサンプル コードは、Custom Secure Metadata Endpoint のサンプルに基づいています。

カスタム バインディングを介してメタデータを取得するには

  1. MEX エンドポイントで使用されているバインディングを特定します。WCF サービスの場合、サービスの構成ファイルにアクセスすることで MEX バインディングを特定できます。この場合、MEX バインディングは、次のサービス構成で定義されています。

    <services>
        <service name="Microsoft.ServiceModel.Samples.CalculatorService"
                behaviorConfiguration="CalculatorServiceBehavior">
         <!-- Use the base address provided by the host. -->
         <endpoint address=""
           binding="wsHttpBinding"
           bindingConfiguration="Binding2"
           contract="Microsoft.ServiceModel.Samples.ICalculator" />
         <endpoint address="mex"
           binding="wsHttpBinding"
           bindingConfiguration="Binding1"
           contract="IMetadataExchange" />
         </service>
     </services>
     <bindings>
       <wsHttpBinding>
         <binding name="Binding1">
           <security mode="Message">
             <message clientCredentialType="Certificate" />
           </security>
         </binding>
         <binding name="Binding2">
           <reliableSession inactivityTimeout="00:01:00" enabled="true" />
           <security mode="Message">
             <message clientCredentialType="Certificate" />
           </security>
         </binding>
       </wsHttpBinding>
     </bindings>
    
  2. クライアント構成ファイルで、同じカスタム バインディングを構成します。ここでクライアントは clientCredentials 動作も定義して、MEX エンドポイントからメタデータを要求するときに、サービスに対する認証で使用する証明書を指定します。カスタム バインディングを介してメタデータを要求するときに Svcutil.exe を使用する場合は、Svcutil.exe の構成ファイル (Svcutil.exe.config) に MEX エンドポイント構成を追加する必要があります。また、エンドポイント構成の名前が MEX エンドポイントのアドレスの URI スキームと一致する必要があります。

      <system.serviceModel>
        <client>
          <endpoint name="http"
                    binding="wsHttpBinding"
                    bindingConfiguration="Binding1"
                    behaviorConfiguration="ClientCertificateBehavior"
                    contract="IMetadataExchange" />
        </client>
        <bindings>
          <wsHttpBinding>
            <binding name="Binding1">
              <security mode="Message">
                <message clientCredentialType="Certificate"/>
              </security>
            </binding>
          </wsHttpBinding>
        </bindings>
        <behaviors>
          <endpointBehaviors>
            <behavior name="ClientCertificateBehavior">
              <clientCredentials>
                <clientCertificate findValue="client.com" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" />
                <serviceCertificate>
                  <authentication certificateValidationMode="PeerOrChainTrust" />
                </serviceCertificate>
              </clientCredentials>
            </behavior>
          </endpointBehaviors>
        </behaviors>  
      </system.serviceModel>
    
  3. MetadataExchangeClient を作成して GetMetadata を呼び出します。これを行うには、構成でカスタム バインディングを指定する方法と、コードでカスタム バインディングを指定する方法の 2 つの方法があります。

    // The custom binding is specified in configuration.
    EndpointAddress mexAddress = new EndpointAddress("https://localhost:8000/ServiceModelSamples/Service/mex");
    
    MetadataExchangeClient mexClient = new MetadataExchangeClient("http");
    mexClient.ResolveMetadataReferences = true;
    MetadataSet mexSet = mexClient.GetMetadata(mexAddress);
    
    // The custom binding is specified in code.
    // Specify the Metadata Exchange binding and its security mode.
    WSHttpBinding mexBinding = new WSHttpBinding(SecurityMode.Message);
    mexBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
    
    // Create a MetadataExchangeClient and set the certificate details.
    MetadataExchangeClient mexClient = new MetadataExchangeClient(mexBinding);
    mexClient.SoapCredentials.ClientCertificate.SetCertificate(
        StoreLocation.CurrentUser, StoreName.My,
        X509FindType.FindBySubjectName, "client.com");
    mexClient.SoapCredentials.ServiceCertificate.Authentication.    CertificateValidationMode =    X509CertificateValidationMode.PeerOrChainTrust;
    mexClient.SoapCredentials.ServiceCertificate.SetDefaultCertificate(
        StoreLocation.CurrentUser, StoreName.TrustedPeople,
        X509FindType.FindBySubjectName, "localhost");
    MetadataExchangeClient mexClient2 = new MetadataExchangeClient(customBinding);
    mexClient2.ResolveMetadataReferences = true;
    MetadataSet mexSet2 = mexClient2.GetMetadata(mexAddress);
    
  4. WsdlImporter を作成して ImportAllEndpoints を呼び出します。

    WsdlImporter importer = new WsdlImporter(mexSet);
    ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
    
  5. この時点で、サービス エンドポイントのコレクションが取得されます。メタデータのインポートの詳細については、「方法 : メタデータをサービス エンドポイントにインポートする」を参照してください。

関連項目

概念

メタデータ