Share via


構成サンプル

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

Dd483343.note(ja-jp,VS.100).gif注 :
このサンプルでは、探索を構成ファイルで実装しています。探索をコードで実装するサンプルについては、「基本的なサンプル」を参照してください。

Dd483343.Important(ja-jp,VS.100).gif 注 :
サンプルは、既にコンピューターにインストールされている場合があります。続行する前に、次の (既定の) ディレクトリを確認してください。

<InstallDrive>:\WF_WCF_Samples

このディレクトリが存在しない場合は、「.NET Framework 4 向けの Windows Communication Foundation (WCF) および Windows Workflow Foundation (WF) のサンプル」にアクセスして、Windows Communication Foundation (WCF) および WF のサンプルをすべてダウンロードしてください。このサンプルは、次のディレクトリに格納されます。

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Discovery\Configuration

サービス構成

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

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

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

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

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

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

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

behaviorConfiguration="endpointBehaviorConfiguration"

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

探索エンドポイントに固有の詳細を制御することもできます。この制御には StandardEndpointsSection を使用します。このサンプルでは、次のコード例に示すように、使用するプロトコルのバージョンを変更し、maxResponseDelay 属性を追加します。

<configuration>
    <system.serviceModel>

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

      <behaviors>

        <serviceBehaviors>
          <behavior name="calculatorServiceBehavior">
            <serviceDiscovery/>
          </behavior>
        </serviceBehaviors>

        <endpointBehaviors>
          <behavior name="endpointBehaviorConfiguration">

            <endpointDiscovery>
              <scopes>
                <add scope="http://www.example.org/calculator"/>
                <add scope="ldap:///ou=engineering,o=exampleorg,c=us"/>
              </scopes>
            </endpointDiscovery>

          </behavior>          
        </endpointBehaviors>

      </behaviors>

      <standardEndpoints>
        <udpDiscoveryEndpoint>
          <!-- Specify the discovery protocol version and maxResponseDelay -->
          <standardEndpoint name="adhocDiscoveryEndpointConfiguration" discoveryVersion="WSDiscovery11" maxResponseDelay="00:00:00.600" />  
        </udpDiscoveryEndpoint>
      </standardEndpoints>

    </system.serviceModel>
</configuration>

クライアント構成

クライアントのアプリケーション構成ファイルでは、dynamicEndpoint 型の standardEndpoint を使用して探索を利用します。クライアントで dynamicEndpoint を使用する場合、探索は自動的に実行されます。探索時にはさまざまな既定の設定が使用されます。たとえば、標準の udpDiscovery エンドポイントを使用して、クライアントによって指定されたコントラクトに一致するサービスを探す discoveryClient などの設定があります。このサンプルでは、この機能を拡張し、クライアントで使用される FindCriteria、および探索に使用される標準の updDiscoveryEndpoint の一部のプロパティを変更します。FindCriteria は、スコープと特定の scopeMatchBy アルゴリズム、およびカスタムの終了条件を使用するように変更します。さらに、このサンプルでは、クライアントで Probe メッセージを使用して XML 要素を送信する方法も示します。最後に、次の構成ファイルに示すように、使用するプロトコルのバージョンや UDP 固有の設定など、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="https://schemas.microsoft.com/ws/2008/06/discovery/rfc" duration="00:00:10" maxResults="1">
              <scopes>
                <add scope="https://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>

このサンプルを使用するには

  1. このサンプルでは HTTP エンドポイントを使用します。このサンプルを実行するには、適切な URL ACL を追加する必要があります (詳細については、「HTTP および HTTPS の構成」を参照してください)。管理者特権で次のコマンドを実行すると、適切な ACL が追加されます。そのままではコマンドが動作しない場合は、代わりに、ドメインとユーザー名を引数に指定して実行してみてください。netsh http add urlacl url=http://+:8000/ user=%DOMAIN%\%UserName%

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

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

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