共用方式為


在組態檔中設定探索

探索中使用四個主要的組態設定群組。 本主題將簡要說明各群組,並且顯示如何設定這些群組的範例。 各節後面會有一個連結,可提供與各領域更為深入的文件。

行為組態

探索會使用服務行為和端點行為。 ServiceDiscoveryBehavior 行為會啟用服務所有端點的探索,並且讓您指定公告端點。 下列範例示範如何加入 ServiceDiscoveryBehavior 並指定公告端點。

<behaviors>
      <serviceBehaviors>
        <behavior name="helloWorldServiceBehavior">
          <serviceDiscovery>
            <announcementEndpoints>
              <endpoint kind="udpAnnouncementEndpoint"/>
            </announcementEndpoints>
          </serviceDiscovery>
        </behavior>
      </serviceBehaviors>

指定此行為之後,請從 <service> 項目參考該行為,如下列範例所示。

<system.serviceModel>
   <services>
      <service name="HelloWorldService" behaviorConfiguration="helloWorldServiceBehavior">
         <!-- Application Endpoint -->
         <endpoint address="endpoint0"
                   binding="basicHttpBinding"
                   contract="IHelloWorldService" />
         <!-- Discovery Endpoints -->
         <endpoint kind="udpDiscoveryEndpoint" />
        </service>
    </service>

為了讓服務能夠探索,您還必須加入探索端點,上述範例即加入了 UdpDiscoveryEndpoint 標準端點。

當您加入公告端點時,也必須將公告接聽程式服務加入至 <services> 項目,如下列範例所示。

<services>
   <service name="HelloWorldService" behaviorConfiguration="helloWorldServiceBehavior">
      <!-- Application Endpoint -->
      <endpoint address="endpoint0"
                binding="basicHttpBinding"
                contract="IHelloWorldService" />
      <!-- Discovery Endpoints -->
      <endpoint kind="udpDiscoveryEndpoint" />
   </service>
   <!-- Announcement Listener Configuration -->
   <service name="AnnouncementListener">
      <endpoint kind="udpAnnouncementEndpoint" />
   </service>

EndpointDiscoveryBehavior 行為可用來啟用或停用特定端點的探索。 下列範例設定服務的兩個應用程式端點,其中一個會啟用探索,另一個則停用探索。 請為各端點加入 EndpointDiscoveryBehavior 行為。

<system.serviceModel>
   <services>
      <service name="HelloWorldService"
               behaviorConfiguration="helloWorldServiceBehavior">

        <!-- Application Endpoints -->
        <endpoint address="endpoint0"
                 binding="basicHttpBinding"
                 contract="IHelloWorldService" 
                 behaviorConfiguration="ep0Behavior" />

        <endpoint address="endpoint1"
                  binding="basicHttpBinding"
                  contract="IHelloWorldService"
                  behaviorConfiguration="ep1Behavior" />
        
        <!-- Discovery Endpoints -->
        <endpoint kind="udpDiscoveryEndpoint" />
      </service>
   </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="helloWorldServiceBehavior">
          <serviceDiscovery />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="ep0Behavior">
          <endpointDiscovery enabled="true"/>
        </behavior>
        <behavior name="ep1Behavior">
          <endpointDiscovery enabled="false"/>
        </behavior>
     </endpointBehaviors>
   </behaviors>

EndpointBehavior 行為也可以用來將自訂中繼資料加入至服務傳回的端點中繼資料。 下列範例顯示如何執行這項工作。

<behavior name="ep4Behavior">
   <endpointDiscovery enabled="true">
      <extensions>
         <CustomMetadata>This is custom metadata.</CustomMetadata>
         <d:Types xmlns:d="https://schemas.xmlsoap.org/ws/2005/04/discovery" xmlns:i="http://printer.example.org/2003/imaging">i:PrintBasic</d:Types>
         <CustomMetadata netsted="true">
            <NestedMetadata>This is a nested custom metadata.</NestedMetadata>
         </CustomMetadata>
      </extensions>
   </endpointDiscovery>
</behavior>

EndpointDiscoveryBehavior 行為也可以用來加入用戶端用來搜尋服務的範圍和類型。 下列範例示範如何在用戶端組態檔中執行這項操作。

<behavior name="ep2Behavior">
   <endpointDiscovery enabled="true">
      <scopes>
         <add scope="https://www.microsoft.com/building42/floor1"/>
         <add scope="ldap:///ou=engineeringo=examplecomc=us"/>
      </scopes>
      <types>
         <add name="test" namespace="http://example.microsoft.com/" />
         <add name="additionalContract" namespace="http://example.microsoft.com/" />
      </types>
   </endpointDiscovery>
</behavior>

如需詳細資訊 ServiceDiscoveryBehaviorEndpointDiscoveryBehavior 的詳細資訊,請參閱 WCF 探索概觀

繫結項目組態

繫結項目組態是用戶端上最有趣的一部分。 您可以使用組態指定尋找準則,用來從 WCF 用戶端應用程式探索服務。 下列範例會建立使用 DiscoveryClient 通道的自訂繫結,並指定包含型別和範圍的尋找準則。 此外,還會指定 Duration 和 MaxResults 屬性的值。

<bindings>
   <customBinding>
      <!-- Binding Configuration for the Application Endpoint -->
      <binding name="discoBindingConfiguration">
         <discoveryClient>
            <endpoint kind="discoveryEndpoint"
                      address="https://localhost:8000/ConfigTest/Discovery"
                      binding="customBinding"
                      bindingConfiguration="httpSoap12WSAddressing10"/>
            <findCriteria duration="00:00:10" maxResults="2">
              <types>
                <add name="IHelloWorldService"/>
              </types>
              <scopes>
                <add scope="https://www.microsoft.com/building42/floor1"/>
              </scopes>            
            </findCriteria>
          </discoveryClient>
          <textMessageEncoding messageVersion="Soap11"/>
          <httpTransport />
        </binding>

用戶端端點必須參考此自訂繫結組態:

<client>
      <endpoint address="https://schemas.microsoft.com/discovery/dynamic"
                binding="customBinding"
                bindingConfiguration="discoBindingConfiguration"
                contract="IHelloWorldService" />
    </client>

如需詳細資訊尋找準則的詳細資訊,請參閱探索尋找與尋找準則。如需詳細資訊探索和繫結項目的詳細資訊,請參閱 WCF 探索概觀

標準端點組態

標準端點是預先定義的端點,其中包含一個或多個屬性 (位址、繫結或合約) 的預設值,或是不可變更的一個或多個屬性值。 .NET 4 隨附 3 個探索相關的標準端點:UdpDiscoveryEndpoint、UpdAnnouncementEndpoint 和 DynamicEndpointUdpDiscoveryEndpoint 是為透過 UDP 多點傳送繫結的探索作業而預先設定的標準端點。 UdpAnnouncementEndpoint 是為透過 UDP 繫結傳送公告訊息而預先設定的標準端點。 DynamicEnpoint 是在執行階段使用探索動態尋找已探索服務之端點位址的標準端點。 標準繫結會使用 <endpoint> 項目指定,該項目中包含指定要加入之標準端點類型的某種屬性。 下列範例示範如何加入 UdpDiscoveryEndpointUpdAnnouncementEndpoint

<services>
   <service name="HelloWorldService">
      <!-- ...  -->        
      <endpoint kind="udpDiscoveryEndpoint" />
   </service>
   <service name="AnnouncementListener">
      <endpoint kind="udpAnnouncementEndpoint" />
   </service>
</services>

標準端點會在 <standardEndpoints> 項目中設定。 下列範例示範如何設定 UdpDiscoveryEndpointUdpAnnouncementEndpoint

<standardEndpoints>
      <udpAnnouncementEndpoint>
        <standardEndpoint 
            name="udpAnnouncementEndpointSettings" 
            multicastAddress="soap.udp://239.255.255.250:3703"  
            maxAnnouncementDelay="00:00:00.800">
          <transportSettings
            duplicateMessageHistoryLength="1028"
            maxPendingMessageCount="10"
            maxMulticastRetransmitCount="3"
            maxUnicastRetransmitCount="2"
            socketReceiveBufferSize="131072"
            timeToLive="2" />
        </standardEndpoint>
      </udpAnnouncementEndpoint>
      <udpDiscoveryEndpoint>
        <standardEndpoint
            name="udpDiscoveryEndpointSettings"
            multicastAddress="soap.udp://239.255.255.252:3704"
            maxResponseDelay="00:00:00.800">
          <transportSettings
            duplicateMessageHistoryLength="2048"
            maxPendingMessageCount="5"
            maxReceivedMessageSize="8192"
            maxBufferPoolSize="262144"/>
        </standardEndpoint>
      </udpDiscoveryEndpoint>

加入標準端點組態之後,請在各端點的 <endpoint> 項目中參考該組態,如下列範例所示。

<services>
   <service name="HelloWorldService">
      <!-- ...  -->        
      <endpoint kind="udpDiscoveryEndpoint" endpointConfiguration="udpDiscoveryEndpointSettings"/>
   </service>
   <service name="AnnouncementListener">
      <endpoint kind="udpAnnouncementEndpoint" endpointConfiguration="udpAnnouncementEndpointSettings" >
   </service>
</services>

與探索中使用的其他標準端點不同的是,您會指定 DynamicEndpoint 的繫結和合約。 下列範例示範如何加入和設定 DynamicEndpoint

<system.serviceModel>
    <client>
      <endpoint kind="dynamicEndpoint" binding="basicHttpBinding" contract="IHelloWorldService" endpointConfiguration="dynamicEndpointConfiguration" />
    </client> 
   <standardEndpoints>
      <dynamicEndpoint>
         <standardEndpoint name="dynamicEndpointConfiguration">
             <discoveryClientSettings>
              <findCriteria scopeMatchBy="https://schemas.microsoft.com/ws/2008/06/discovery/rfc" duration="00:00:10" maxResults="2">
                 <types>
                   <add name="IHelloWorldService"/>
                 </types>
                 <scopes>
                   <add scope="https://www.microsoft.com/building42/floor1"/>
                 </scopes>
                 <extensions>
                   <CustomMetadata>This is custom metadata.</CustomMetadata>        
                 </extensions>
               </findCriteria>
             </discoveryClientSettings>
           </standardEndpoint>
         </dynamicEndpoint>
   </standardEndpoints>
</system.ServiceModel>

如需詳細資訊 標準端點的詳細資訊,請參閱 標準端點