共用方式為


組態範例

組態範例示範如何使用組態檔來使服務可被發現。

服務組態

此範例中的組態檔示範兩個功能:

  • 讓服務能夠透過標準 UdpDiscoveryEndpoint 被發現。

  • 調整服務應用程式端點的探索相關信息,以及調整標準端點上的一些探索相關設定。

若要啟用探索,必須在服務的應用程式組態檔中進行一些變更:

  • 探索端點必須新增至 <service> 元素。 這是標準 UdpDiscoveryEndpoint 端點。 這是運行時與發現服務相關聯的系統端點。 探索服務會接聽此端點上的訊息。

  • <serviceDiscovery> 行為被新增至 <serviceBehaviors> 區段。 這使得服務能在執行時被發現,並利用前述的發現端點來監聽發現 ProbeResolve 訊息。 透過這兩個新增項目,服務可在指定的發現端點上被發現。

下列設定片段顯示了一個已定義應用程式端點和探索端點的服務:

<services>
        <service name="Microsoft.Samples.Discovery.CalculatorService"
                 behaviorConfiguration="calculatorServiceBehavior">
          <endpoint address=""
                    binding="wsHttpBinding"
                    contract="Microsoft.Samples.Discovery.ICalculatorService"
                    behaviorConfiguration="endpointBehaviorConfiguration" />
          <endpoint name="udpDiscovery"
                    kind="udpDiscoveryEndpoint"
                endpointConfiguration="adhocDiscoveryEndpointConfiguration"/>        </service>
      </services>

若要利用公告,您必須新增公告端點。 若要這樣做,請修改組態檔,如下列程式代碼所示。

<serviceDiscovery>
            <announcementEndpoints>
              <endpoint kind="udpAnnouncementEndpoint"/>
            </announcementEndpoints>
          </serviceDiscovery>

將公告端點新增到探索服務的行為中,會為此服務建立預設的公告客戶端。 這可確保服務會在服務開啟和關閉時,分別傳送在線和離線公告。

此組態檔除了執行這些簡單步驟外,還會修改其他行為。 您可以使用特定端點來控制探索相關信息。 也就是說,使用者可以控制是否可以探索端點,而且使用者也可以使用 和自定義 XML 元數據來標記該端點 Scopes 。 若要這樣做,用戶必須將屬性新增 behaviorConfiguration 至應用程式端點。 在此情況下,下列屬性會新增至應用程式端點。

behaviorConfiguration="endpointBehaviorConfiguration"

現在,透過行為組態專案,您可以控制探索相關的屬性。 在此情況下,會將兩個範圍新增至應用程式端點。

<endpointBehaviors>
          <behavior name="endpointBehaviorConfiguration">
            <endpointDiscovery>
              <scopes>
                <add scope="http://www.example.com/calculator"/>
                <add scope="ldap:///ou=engineering,o=examplecom,c=us"/>
              </scopes>
            </endpointDiscovery>

          </behavior>
        </endpointBehaviors>

如需範圍的詳細資訊,請參閱「探索尋找」和「FindCriteria」。

您也可以控制探索端點的特定詳細數據。 這是透過StandardEndpointsSection來完成的。 在此範例中,所使用的通訊協定版本已修改,並新增 maxResponseDelay 屬性,如下列程式代碼範例所示。

<standardEndpoints>
   <udpDiscoveryEndpoint>
      <standardEndpoint name="adhocDiscoveryEndpointConfiguration" discoveryVersion="WSDiscovery11" maxResponseDelay="00:00:00.600" />
   </udpDiscoveryEndpoint>
</standardEndpoints>

以下是此範例中使用的完整組態檔:

<configuration>
    <system.serviceModel>

      <services>
        <service name="Microsoft.Samples.Discovery.CalculatorService"
                 behaviorConfiguration="calculatorServiceBehavior">
          <endpoint address=""
                    binding="wsHttpBinding"
                    contract="Microsoft.Samples.Discovery.ICalculatorService"
                    behaviorConfiguration="endpointBehaviorConfiguration" />
         <!-- Define the discovery endpoint -->
<endpoint name="udpDiscovery" kind="udpDiscoveryEndpoint" endpointConfiguration="adhocDiscoveryEndpointConfiguration"/>        </service>
      </services>

      <behaviors>

        <serviceBehaviors>
          <behavior name="calculatorServiceBehavior">

            <!-- Add an announcement endpoint -->
            <serviceDiscovery>
              <announcementEndpoints>
                <endpoint kind="udpAnnouncementEndpoint"/>
              </announcementEndpoints>
            </serviceDiscovery>
          </behavior>
        </serviceBehaviors>

        <endpointBehaviors>
          <behavior name="endpointBehaviorConfiguration">
            <!-- Add scopes used to identify the service -->
            <endpointDiscovery>
              <scopes>
                <add scope="http://www.example.com/calculator"/>
                <add scope="ldap:///ou=engineering,o=examplecom,c=us"/>
              </scopes>
            </endpointDiscovery>

          </behavior>
        </endpointBehaviors>

      </behaviors>

      <standardEndpoints>
        <udpDiscoveryEndpoint>
         <!-- Configure the UDP discovery endpoint -->
          <standardEndpoint name="adhocDiscoveryEndpointConfiguration" discoveryVersion="WSDiscovery11" maxResponseDelay="00:00:00.600" />
        </udpDiscoveryEndpoint>
      </standardEndpoints>

    </system.serviceModel>
</configuration>

客戶端設定

在客戶端的應用程式組態檔中,使用類型 standardEndpointdynamicEndpoint 來實現發現,如以下配置片段所示。

<client>
   <!--  Create an endpoint, make kind="dynamicEndpoint" and use the endpointConfiguration to change settings of DynamicEndpoint -->
   <endpoint name="calculatorEndpoint"
             binding="wsHttpBinding"
             contract="ICalculatorService"
             kind ="dynamicEndpoint"
             endpointConfiguration="dynamicEndpointConfiguration">
   </endpoint>
</client>

當客戶端使用 dynamicEndpoint 時,執行階段會自動進行探索。 探索期間會使用各種設定,例如 區段中定義的 discoveryClientSettings 設定,其會指定要使用的探索端點類型:

<endpoint kind="udpDiscoveryEndpoint" endpointConfiguration="adhocDiscoveryEndpointConfiguration" />

用來搜尋服務的尋找準則:

<!-- Add Scopes, ScopeMatchBy, Extensions and termination criteria in FindCriteria -->
<findCriteria scopeMatchBy="http://schemas.microsoft.com/ws/2008/06/discovery/rfc" duration="00:00:10" maxResults="1">
   <scopes>
      <add scope="http://www.microsoft.com/building42/floor1"/>
   </scopes>
   <!-- These extensions are sent from the client to the service as part of the probe message -->
   <extensions>
      <CustomMetadata>This is custom metadata that is sent to the service along with the client's find request.</CustomMetadata>
   </extensions>
</findCriteria>

此範例會擴充這項功能,並修改客戶端所使用的FindCriteria,以及用於探索的標準updDiscoveryEndpoint的部分屬性。 FindCriteria被修改為使用一個範圍和特定的scopeMatchBy演算法,以及自定義的終止準則。 此外,此範例也示範了用戶如何透過 Probe 訊息傳送 XML 元素。 最後,對 UdpDiscoveryEndpoint 進行了一些變更,例如所使用的通訊協定版本和 UDP 專用設定,如下列組態檔案所示。

<udpDiscoveryEndpoint>
        <!-- Specify the discovery protocol version and UDP transport settings. -->
        <standardEndpoint name="adhocDiscoveryEndpointConfiguration" discoveryVersion="WSDiscovery11">
          <transportSettings duplicateMessageHistoryLength="2048"
                             maxPendingMessageCount="5"
                             maxReceivedMessageSize="8192"
                             maxBufferPoolSize="262144"/>
        </standardEndpoint>
      </udpDiscoveryEndpoint>

以下是範例中使用的完整用戶端組態。

<configuration>
  <system.serviceModel>

    <client>
      <!--  Create an endpoint, make kind="dynamicEndpoint" and use the endpointConfiguration to change settings of DynamicEndpoint -->
      <endpoint name="calculatorEndpoint"
                binding="wsHttpBinding"
                contract="ICalculatorService"
                kind ="dynamicEndpoint"
                endpointConfiguration="dynamicEndpointConfiguration">
      </endpoint>
    </client>

    <standardEndpoints>

      <dynamicEndpoint>
        <standardEndpoint name="dynamicEndpointConfiguration">
          <discoveryClientSettings>
            <!-- Controls where the discovery happens. In this case, Probe message is sent over UdpDiscoveryEndpoint. -->
            <endpoint kind="udpDiscoveryEndpoint" endpointConfiguration="adhocDiscoveryEndpointConfiguration" />

            <!-- Add Scopes, ScopeMatchBy, Extensions and termination criteria in FindCriteria -->
            <findCriteria scopeMatchBy="http://schemas.microsoft.com/ws/2008/06/discovery/rfc" duration="00:00:10" maxResults="1">
              <scopes>
                <add scope="http://www.microsoft.com/building42/floor1"/>
              </scopes>
              <!-- These extensions are sent from the client to the service as part of the probe message -->
              <extensions>
                <CustomMetadata>This is custom metadata that is sent to the service along with the client's find request.</CustomMetadata>
              </extensions>
            </findCriteria>
          </discoveryClientSettings>
        </standardEndpoint>
      </dynamicEndpoint>

      <udpDiscoveryEndpoint>
        <!-- Specify the discovery protocol version and UDP transport settings. -->
        <standardEndpoint name="adhocDiscoveryEndpointConfiguration" discoveryVersion="WSDiscovery11">
          <transportSettings duplicateMessageHistoryLength="2048"
                             maxPendingMessageCount="5"
                             maxReceivedMessageSize="8192"
                             maxBufferPoolSize="262144"/>
        </standardEndpoint>
      </udpDiscoveryEndpoint>

    </standardEndpoints>

  </system.serviceModel>
</configuration>

若要使用此範例

  1. 此範例會使用 HTTP 端點並執行此範例,必須新增適當的 URL ACL。 如需詳細資訊,請參閱 設定 HTTP 和 HTTPS。 以管理員權限執行下列命令應該可以新增適當的存取控制清單。 如果命令無法如往般運作,您可以將網域和用戶名稱取代為下列自變數。 netsh http add urlacl url=http://+:8000/ user=%DOMAIN%\%UserName%

  2. 建置解決方案。

  3. 從組建目錄執行服務可執行檔。

  4. 執行客戶端可執行檔。 請注意,客戶端能夠找到服務。