共用方式為


HOW TO:設定自訂 WS-Metadata Exchange 繫結

這個主題會說明如何設定自訂的 WS-Metadata Exchange 繫結。Windows Communication Foundation (WCF) 包括四個系統定義的中繼資料繫結,但您可以使用想要的任何繫結來發行中繼資料。這個主題會告訴您如何使用 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. 若要驗證中繼資料交換端點是否正常運作,請在用戶端組態檔中新增端點標記:

    <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

    serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, mexAddress);
    
  5. 若要驗證中繼資料交換端點是否正常運作,請在用戶端組態檔中新增端點標記:

    <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