共用方式為


HOW TO:使用 MetadataResolver 來動態取得繫結中繼資料

本主題示範如何使用 MetadataResolver 類別來動態取得繫結中繼資料。

若要動態取得繫結中繼資料

  1. 建立具有中繼資料端點位址的 EndpointAddress 物件。

    EndpointAddress metaAddress
      = new EndpointAddress(new   Uri("https://localhost:8080/SampleService/mex"));
    
  2. 呼叫 Resolve,它會傳入服務類型和中繼資料端點位址。這麼做會傳回實作指定之合約的端點集合。只有繫結資訊會從中繼資料匯出,不會匯出合約資訊。並改用所提供的合約。

    ServiceEndpointCollection endpoints = MetadataResolver.Resolve(typeof(SampleServiceClient),metaAddress);
    
  3. 您接著便可以逐一查看服務端點的集合,以擷取所需的繫結資訊。下列程式碼會逐一查看端點、建立服務用戶端物件 (此物件會傳入與目前端點關聯的繫結和位址),然後再呼叫服務上的方法。

    foreach (ServiceEndpoint point in endpoints)
    {
       if (point != null)
       {
          // Create a new wcfClient using retrieved endpoints.
          using (wcfClient = new SampleServiceClient(point.Binding, point.Address))
          {
             Console.WriteLine(
               wcfClient.SampleMethod("Client used the "
               + point.Address.ToString() + " address."));
          }
      }
    }
    

請參閱

概念

中繼資料