次の方法で共有


構成サンプル

構成サンプルでは、構成ファイルを使用してサービスを探索可能にする方法を示します。

サービス構成

このサンプルの構成ファイルでは、次の 2 つの機能を示します。

  • 標準の UdpDiscoveryEndpoint を介してサービスを探索できるようにします。

  • サービスのアプリケーション エンドポイントに対する探索関連の情報の調整、および標準エンドポイントに対する探索関連の設定の調整を行います。

探索を有効にするには、サービスのアプリケーション構成ファイルで次の変更を行う必要があります。

  • 探索エンドポイントを <service> 要素に追加する必要があります。 これは標準の UdpDiscoveryEndpoint エンドポイントです。 このエンドポイントが、ランタイムによって探索サービスに関連付けられるシステム エンドポイントになります。 探索サービスは、このエンドポイント上でメッセージをリッスンします。

  • <serviceDiscovery> 動作を <serviceBehaviors> セクションに追加します。 これにより、サービスが実行時に探索されるようになり、前述の探索エンドポイントを使用して探索の Probe および Resolve メッセージをリッスンするようになります。 この 2 つの追加により、指定した探索エンドポイントでサービスが探索可能になります。

次の構成スニペットでは、アプリケーション エンドポイントのサービスと定義された探索エンドポイントが示されています。

<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>

アナウンス エンドポイントを探索サービス動作に追加することにより、サービスの既定のアナウンス クライアントが作成されます。 これによって、サービスは、サービスが開いたときおよび閉じたときにそれぞれオンラインおよびオフラインのアナウンスを送信することが保証されます。

この構成ファイルは、さらに動作を変更することによって、より高度な機能を提供できます。 特定のエンドポイントを使用することで、探索関連の情報を制御できます。 つまり、ユーザーは、エンドポイントを探索できるかどうかを制御できるだけでなく、そのエンドポイントを Scopes およびカスタム XML メタデータでマークすることもできます。 これを行うには、behaviorConfiguration プロパティをアプリケーション エンドポイントに追加する必要があります。 この場合、次のプロパティをアプリケーション エンドポイントに追加します。

behaviorConfiguration="endpointBehaviorConfiguration"

これで、この動作構成要素を通じて、探索関連の属性を制御できます。 この場合、2 つのスコープがアプリケーション エンドポイントに追加されます。

<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>

クライアント構成

クライアントのアプリケーション構成ファイルでは、standardEndpoint 型の dynamicEndpoint を使用して次の構成スニペットに示すように探索を利用します。

<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 要素を送信する方法も示します。 最後に、次の構成ファイルに示すように、使用するプロトコルのバージョンや UDP 固有の設定など、UdpDiscoveryEndpoint の変更をいくつか行います。

<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 の構成」を参照してください。 管理特権で次のコマンドを実行すると、適切な ACL が追加されます。 そのままではコマンドが動作しない場合は、代わりに、ドメインとユーザー名を引数に指定して実行してみてください。 netsh http add urlacl url=http://+:8000/ user=%DOMAIN%\%UserName%

  2. ソリューションをビルドします。

  3. ビルド ディレクトリからサービス実行可能ファイルを実行します。

  4. クライアント実行可能ファイルを実行します。 クライアントでサービスを検索できることに注意してください。