Share via


Exercise 6: Discovery Announcements

Most chat applications notify you when other users sign in. This application can discover other users but it would be better if you were notified when other users sign in. Discovery supports this feature with announcements. Using Discovery APIs service can announce their presence on the network; this functionality is used in the chat applications to notify peers of their arrival.

Task 0 – Opening the Solution

To begin this exercise you can use the solution you finished from Exercise 5. Alternatively, you can follow the following steps to begin with Exercise 6.

  1. Open the starting solution for Exercise 6 located under the Source\Ex6-DiscoveryAnnouncements\Begin (choosing the folder that matches the language of your preference.) Use it as the starting point for this exercise.
  2. Press CTRL+SHIFT+B to build the solution.

Task 1 – Enabling Announcement Endpoint

  1. Open the App.config file in the DiscoveryChat project.
  2. To take advantage of announcements, you will need to add an announcement endpoint. To do this, locate the DiscoveryBehavior behavior you added earlier in this lab and modify it as shown in the following code.

    (Code Snippet - What is new in WCF4 Lab – DiscoveryBehavior udpEndpoint XML)

    App.config

    <behavior name="DiscoveryBehavior">
    <serviceDiscovery> <announcementEndpoints> <endpoint name="udpEndpointName" kind="udpAnnouncementEndpoint"/> </announcementEndpoints> </serviceDiscovery>FakePre-f64495a5ef474268a3f8f04f165cf29f-e88c5dd1fe5a41529abb01c425c1a2dc

    Note:
    Announcement Endpoint

    Adding an announcement endpoint to the discovery service behavior creates a default announcement client for the service. This ensures that the service will send an online and offline announcement when the service is opened and closed respectively, the announcements will be sent out on the endpoint specified.

  3. Now you need to add an announcement service to your code to receive announcement messages. Open SimpleChat.cs (C#) or SimpleChat.vb (Visual Basic), and declare the following member fields; you can do that after the discoveryClient member declaration.

    (Code Snippet - What is new in WCF4 Lab – AnnouncementService Member CSharp)

    C#

    private AnnouncementService announcementService; private ServiceHost announcementServiceHost;

    (Code Snippet - What is new in WCF4 Lab – AnnouncementService Member VB)

    Visual Basic

    Private announcementService As AnnouncementService Private announcementServiceHost As ServiceHost

  4. Create the OpenAnnouncementService method as shown in the following code.

    (Code Snippet - What is new in WCF4 Lab – OpenAnnouncementService Method CSharp)

    C#

    private void OpenAnnouncementService() { this.announcementService = new AnnouncementService(); // Add event handlers this.announcementService.OnlineAnnouncementReceived += new EventHandler<AnnouncementEventArgs>(this.OnOnlineAnnouncement); this.announcementService.OfflineAnnouncementReceived += new EventHandler<AnnouncementEventArgs>(this.OnOfflineAnnouncement); // Create the service host with a singleton this.announcementServiceHost = new ServiceHost(this.announcementService); // Add the announcement endpoint this.announcementServiceHost.AddServiceEndpoint(new UdpAnnouncementEndpoint()); // Open the host async this.announcementServiceHost.BeginOpen( (result) => { announcementServiceHost.EndOpen(result); }, null); }
    FakePre-250f29d98f3449de90cc7efea0f7d68b-dbfb81d8ab2c4382b5944822ca68c231FakePre-4b9f50b38376421c8fdb7a3ab1cf2db2-deb1487b086c4c34b5f383de28b0c7d0FakePre-cf0e30dca7d4493eb2c21993dc377fe5-15e39e9e96de442e866f9f693c3c99f4
    

    (Code Snippet - What is new in WCF4 Lab – OpenAnnouncementService Method VB)

    Visual Basic

    Private Sub OpenAnnouncementService() Me.announcementService = New AnnouncementService() ' Add event handlers AddHandler announcementService.OnlineAnnouncementReceived, AddressOf OnOnlineAnnouncement AddHandler announcementService.OfflineAnnouncementReceived, AddressOf OnOfflineAnnouncement ' Create the service host with a singleton Me.announcementServiceHost = New ServiceHost(Me.announcementService) ' Add the announcement endpoint Me.announcementServiceHost.AddServiceEndpoint(New UdpAnnouncementEndpoint()) ' Open the host async Me.announcementServiceHost.BeginOpen(Sub(result) announcementServiceHost.EndOpen(result), Nothing) End Sub
    FakePre-f9fa3b3e03ca4f6290d304292dcba43e-d736366fdfe4438ba246744044c3baaaFakePre-c1e3a3b491e7419d80e35f35ee967ef2-8dd3df204d804945b94d4b8ca5b499a9FakePre-527907f4a6924242abac4f2e608454a4-3bef11cc26c648a8be90886b6a343d52
    

    Note:
    Announcement Service

    The self-hosted implementation of the announcement service exposes two different events you might be interested in when using Announcements: OnlineAnnouncementReceived and OfflineAnnouncementReceived. Those events are fired when Online (Hello) and Offline (Bye) announcement messages are received respectively.

  5. Now you need to implement the handlers for these announcements. When new users come online, you will add them to the list. Add the OnOnlineAnnouncement method as shown in the following code.

    (Code Snippet - What is new in WCF4 Lab – OnOnlineAnnouncement Method CSharp)

    C#

    private void OnOnlineAnnouncement(object sender, AnnouncementEventArgs e) { EndpointDiscoveryMetadata metadata = e.EndpointDiscoveryMetadata; // You are looking for services that // implement the ISimpleChatService contract FindCriteria criteria = new FindCriteria(typeof(ISimpleChatService)); if (criteria.IsMatch(metadata)) { if (this.GetUser(metadata.Address.Uri) == null) { this.PopulateUserList(metadata); } } }
    FakePre-63d81f01a9bd43cebf94b07f28a9c7f2-e18ef7b016b94a53b1060f0dd0b28043
    

    (Code Snippet - What is new in WCF4 Lab – OnOnlineAnnouncement Method VB)

    Visual Basic

    Private Sub OnOnlineAnnouncement(ByVal sender As Object, ByVal e As AnnouncementEventArgs) Dim metadata As EndpointDiscoveryMetadata = e.EndpointDiscoveryMetadata ' You are looking for services that ' implement the ISimpleChatService contract Dim criteria As New FindCriteria(GetType(ISimpleChatService)) If criteria.IsMatch(metadata) Then If Me.GetUser(metadata.Address.Uri) Is Nothing Then Me.PopulateUserList(metadata) End If End If End Sub
    FakePre-ef3868c368f84454b3bb98af4d23e7f9-887898fcb8df4744b54f7da149c1b909
    

  6. When users go offline you will remove them and close any active chat windows. Add the OnOfflineAnnouncement method as shown in the following code.

    (Code Snippet - What is new in WCF4 Lab – OnOfflineAnnouncement Method CSharp)

    C#

    private void OnOfflineAnnouncement(object sender, AnnouncementEventArgs e) { EndpointDiscoveryMetadata metadata = e.EndpointDiscoveryMetadata; FindCriteria criteria = new FindCriteria(typeof(ISimpleChatService)); if (criteria.IsMatch(metadata)) { this.RemoveUser(metadata.Address.Uri); } }
    FakePre-a5649b5b3612432e862344fb1a63ed70-90985caa21dd4700843bf6577ffa777e
    

    (Code Snippet - What is new in WCF4 Lab – OnOfflineAnnouncement Method VB)

    Visual Basic

    Private Sub OnOfflineAnnouncement(ByVal sender As Object, ByVal e As AnnouncementEventArgs) Dim metadata As EndpointDiscoveryMetadata = e.EndpointDiscoveryMetadata Dim criteria As New FindCriteria(GetType(ISimpleChatService)) If criteria.IsMatch(metadata) Then Me.RemoveUser(metadata.Address.Uri) End If End Sub
    FakePre-212bc1bd91864895a433b0a0cc6b4a55-cb36c1c8c5db41a8aa45d123fa545ed2
    

  7. Locate the OpenServices method, and add a call to OpenAnnouncementService service at the end of the method implementation.

    C#

        this.ShowStatus("Opening chat service...");
    FakePre-458e3110383f4024a98fa9b531f86d58-8759eff8d3e24eea870ccd4bca22939aFakePre-3cc2a02991b44b5bac865bc0381cb0cc-50a49c11251e456cad10ec8d0ef439c8FakePre-553de51c21034ee79d0079f68b948210-80b95ff62fbf4e5bb99bc2b481753220FakePre-f63d595e4ca4433184ea412db94936ea-0ef3fdf003cd46e780a1dc8736501084FakePre-80a9d95231204645a6db935e24637080-27d33788838f43a0b79d2ed7fba69ceaFakePre-6a35371a2d984c118c1618632e5d9b15-4db7b83d262b47cd8736ef75c54f2f7fFakePre-b1743bfd061c4db49a31d0228f31dfcb-3096e154aa864a70842551f7c5c6872dFakePre-6da48ccab12c4e77be3664d2bc344b51-88ccde85e9394bb69e1c5a3a503b161f this.OpenAnnouncementService();FakePre-cc433cc9edc3410b9617e5cb9442a0df-aa0695a60b78438d866c4e44ae8f5cc2

    Visual Basic

        Me.ShowStatus("Opening chat service...")
    FakePre-40076b4e864b4971b8f3fb2786630ce7-baca2cb79d1f4daeb9ceb9e4eb74734dFakePre-2e20e60b8c5b49d3979e00a3cc6acebf-98be54b4c2e04ac1b399e720b9d6e11fFakePre-34cb4651befa46cb9bf6bb6c917964a7-27aa53aef430444a908971a440478efdFakePre-c9fed65a44db4def80ba26b91994927a-b8de7be0da354ce7944096fdddb664afFakePre-2feb984a8d0b4a72b9581feadeb26a6a-91fd74fd555945038ed10f3bbbf532e4 Me.OpenAnnouncementService()FakePre-c411e3b569304597b54d04828a4923ab-4a85f85921004732a36c9d0205cb248b

  8. Press CTRL+SHIFT+B to build the solution.

Next Step

Exercise 6: Verification