配置示例演示如何使用配置文件使服务可发现。
服务配置
此示例中的配置文件演示了两个功能:
使服务在标准 UdpDiscoveryEndpoint 上成为可发现的服务。
调整服务应用程序终结点的发现相关信息,并在标准终结点上调整一些与发现相关的设置。
若要启用发现,必须在服务的应用程序配置文件中进行一些更改:
必须将发现终结点添加到
<service>
元素。 这是一个标准 UdpDiscoveryEndpoint 终结点。 这是运行时与发现服务关联的系统终结点。 发现服务侦听此终结点上的消息。将
<serviceDiscovery>
行为添加到<serviceBehaviors>
部分。 这使该服务在运行时可被发现,并使用前面提到的发现终结点来侦听发现Probe
和Resolve
消息。 通过这两个添加项,可以在指定的发现终结点发现服务。
以下配置片段显示了一个服务,其中定义了应用程序终结点和发现终结点:
<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"
现在,通过行为配置元素,可以控制与发现相关的属性。 在这种情况下,会将两个作用域添加到应用程序终结点。
<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>
有关范围的详细信息,请参阅 Discovery Find 和 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 元素。 最后,对 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>
使用此示例
此示例使用 HTTP 终结点并运行此示例,必须添加适当的 URL ACL。 有关详细信息,请参阅 配置 HTTP 和 HTTPS。 在提升的权限下执行以下命令应添加相应的 ACL。 如果命令不按原样工作,则可能需要将域和用户名替换为以下参数。
netsh http add urlacl url=http://+:8000/ user=%DOMAIN%\%UserName%
生成解决方案。
从生成目录运行服务可执行文件。
运行客户端可执行文件。 请注意,客户端能够找到该服务。