[WCF-Discovery]让服务自动发送上/下线通知[实例篇]

长平狐 发布于 2012/09/04 11:10
阅读 60
收藏 0

image在《原理篇》我们对客户端如何监听通知,以及服务在上下线时如何发送通知从原理上进行了深入地剖析。我们现在通过一个简单的实例演示如何通过ServiceDiscoveryBehavior服务行为为寄宿的服务添加一个实现上/下线通知的AnnouncementEndpoint终结点,以及客户端如何通过对AnnouncementService服务的寄宿实现对通知的监听和接收。[源代码从这里下载]

我们采用如右图所示的解决方案结构,其中Service.Interface、Service(控制台程序)和Client(控制台程序),用于用于定义服务契约、服务类型(服务寄宿)和客户端监听程序。并且直接使用我们熟悉的CalculatorService。现在我们为该服务的寄宿定义如下一段配置。我们定义了一个包含ServiceDiscoveryBehavior行为的默认服务行为,并且一个UdpAnnouncementEndpoint终结点被添加到了ServiceDiscoveryBehavior的AnnouncementEndpoints集合之中。

   1: <configuration>
   2:   <system.serviceModel>
   3:     <services>
   4:       <service name="Artech.WcfServices.Service.CalculatorService">
   5:         <endpoint address="http://127.0.0.1:3721/calculatorservice" 
   6:                   binding="ws2007HttpBinding" 
   7:                   contract="Artech.WcfServices.Service.Interface.ICalculator" />
   8:       </service>
   9:     </services>
  10:     <behaviors>
  11:       <serviceBehaviors>
  12:         <behavior>
  13:           <serviceDiscovery>
  14:             <announcementEndpoints>
  15:               <endpoint kind="udpAnnouncementEndpoint" />
  16:             </announcementEndpoints>
  17:           </serviceDiscovery>
  18:         </behavior>
  19:       </serviceBehaviors>
  20:     </behaviors>
  21:   </system.serviceModel>
  22: </configuration>

然后通过如下一段简短的代码对CalculatorService进行寄宿,与之前的不同之处在于输出了一段程序退出的提示性文字。当服务寄宿程序启动后输入任何字符,服务将会被关闭(离线)。

   1: using System;
   2: using System.ServiceModel;
   3: namespace Artech.WcfServices.Service
   4: {
   5:     class Program
   6:     {
   7:         static void Main(string[] args)
   8:         {
   9:             using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
  10:             {
  11:                 host.Open();
  12:                 Console.WriteLine("Enter any key to exit.");
  13:                 Console.Read();
  14:             }
  15:         }
  16:     }
  17: }

接下来我们需要在客户端通过寄宿AnnouncementService服务来监听CalculatorService服务启动和关闭自动发出的通知。我们先通过如下所示的一段配置为寄宿的AnnouncementService添加一个UdpAnnouncementEndpoint终结点。

   1: <configuration>   
   2:     <system.serviceModel>
   3:         <services>
   4:             <service name="System.ServiceModel.Discovery.AnnouncementService">
   5:                 <endpoint kind="udpAnnouncementEndpoint" />
   6:             </service>
   7:         </services>
   8:     </system.serviceModel>
   9: </configuration>

AnnouncementService服务通过如下的代码进行寄宿。由于AnnouncementService被定义成一个单例服务,我们就可以直接针对一个预先创建好的AnnouncementService实例来创建用于服务寄宿的ServiceHost。在服务开启之前,我们注册了AnnouncementService的OnlineAnnouncementReceived和OfflineAnnouncementReceived两个事件,在它接收到目标服务上/下线通知的时候会输出目标服务终结点的地址和契约名称。

   1: using System;
   2: using System.ServiceModel;
   3: using System.ServiceModel.Discovery;
   4: namespace Artech.WcfServices.Client
   5: {
   6:     class Program
   7:     {
   8:         static void Main(string[] args)
   9:         {
  10:             AnnouncementService announcementService = new AnnouncementService();
  11:             announcementService.OnlineAnnouncementReceived += (sender, e) =>
  12:                 {
  13:                     string contractTypes = string.Empty;
  14:                     Console.WriteLine("Receive Service Online Announcement.");
  15:                     Console.WriteLine("\tAddress: {0}", e.EndpointDiscoveryMetadata.Address.Uri);
  16:                     Console.WriteLine("\tContract: {0}", e.EndpointDiscoveryMetadata.ContractTypeNames[0]);
  17:                 };
  18:             announcementService.OfflineAnnouncementReceived += (sender, e) =>
  19:             {
  20:                 string contractTypes = string.Empty;
  21:                 Console.WriteLine("Receive Service Offline Announcement.");
  22:                 Console.WriteLine("\tAddress: {0}", e.EndpointDiscoveryMetadata.Address.Uri);
  23:                 Console.WriteLine("\tContract: {0}", e.EndpointDiscoveryMetadata.ContractTypeNames[0]);
  24:             };
  25:             using (ServiceHost host = new ServiceHost(announcementService))
  26:             {
  27:                 host.Open();
  28:                 Console.Read();
  29:             }
  30:         }
  31:     }
  32: }

客户端和服务端所有的配置和编码工作完成之后,先启动客户端开启通知监听服务。然后开启服务端启动服务CalculatorService,最后输入任意键(不要直接关闭控制台窗口)退出服务端程序。此时你会发现客户端的控制台上具有如下的输出。这段文字的第一部分代表CalculatorService启动的时候(ServiceHost的Open方法被执行之后)发出的上线通知,后一部分代表服务关闭(ServiceHost的Dispose方法执行之后)发出的离线通知。

   1: Receive Service Online Announcement.
   2:         Address: http://127.0.0.1:3721/calculatorservice
   3:         Contract: http://www.artech.com/:ICalculator
   4: Receive Service Offline Announcement.
   5:         Address: http://127.0.0.1:3721/calculatorservice
   6:         Contract: http://www.artech.com/:ICalculator

原文链接:http://www.cnblogs.com/artech/archive/2011/10/25/2223637.html
加载中
返回顶部
顶部