Announcing Online Events in WCF Service


Today, we will implement one of the interesting concept in WCF of how endpoints can be announced and provide availability for the clients to listen who are consuming this service.

Question Arises: What is Announcement Service in WCF?

Endpoints can be announcement as a collection which is available for the client who is consuming the service will be able to listen when comes online/offline Events. Let's get this concept implemented such that we will have a better idea on this

So, let's get it started off now.

The Complete Code of IService1.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string Hello(string val);
        [OperationContract]
        string Bye(string val);
    }

The Complete Code of Service1.svc.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {

        public string Hello(string val)
        {
            return val;
        }

        public string Bye(string val)
        {
            return val;
        }
    }
}
         

The Complete Code of Web.Config looks like this:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <
compilation debug="true" targetFramework="4.0" />
  </system.web>
  <
system.serviceModel>
    <
behaviors>
      <
serviceBehaviors>
        <
behavior name="Application">
          <serviceDiscovery>
            <
announcementEndpoints>
              <
endpoint kind="udpAnnouncementEndpoint"></endpoint>
            </announcementEndpoints>
          </
serviceDiscovery>
          <!--
To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <
serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <
serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </
serviceBehaviors>
    </
behaviors>
    <
serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
      <
service name="WcfService2.Service1" behaviorConfiguration="Application">
        <endpoint address="/Simple" binding="basicHttpBinding" contract="WcfService2.IService1"></endpoint>
        <endpoint  kind="udpDiscoveryEndpoint"></endpoint>
        <host>
          <
baseAddresses>
            <
add baseAddress="http://localhost:52429/Service1.svc"/>
          </baseAddresses>
        </
host>
      </
service>
    </
services>

  </system.serviceModel>
  <
system.webServer>
    <
modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

The Complete Code of Program.cs looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.ServiceReference1;
using System.ServiceModel.Discovery;
using System.ServiceModel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an AnnouncementService instance
            AnnouncementService announcementService = new AnnouncementService();

            // Subscribe the announcement events
            announcementService.OnlineAnnouncementReceived += OnOnlineEvent;
            announcementService.OfflineAnnouncementReceived += OnOfflineEvent;

            // Create ServiceHost for the AnnouncementService
            using (ServiceHost announcementServiceHost = new ServiceHost(announcementService))
            {
                // Listen for the announcements sent over UDP multicast
                announcementServiceHost.AddServiceEndpoint(new UdpAnnouncementEndpoint());
                announcementServiceHost.Open();
                Service1Client obj = new Service1Client();

                Console.WriteLine("The Online is " + obj.Hello("Vijay prativadi"));
                Console.WriteLine("The Offline is " + obj.Bye("Vijay prativadi"));

                Console.WriteLine("Press <ENTER> to terminate.");

                Console.ReadLine();
                announcementServiceHost.Close();

            }

        }
        static void OnOnlineEvent(object sender, AnnouncementEventArgs e)
        {

            Console.WriteLine("Received an online announcement from {0}",
        e.EndpointDiscoveryMetadata.Address);
            Console.WriteLine("Received an online announcement from {0}", e.EndpointDiscoveryMetadata.ContractTypeNames.ToString());
            Console.WriteLine("Received an online announcement from {0}", e.EndpointDiscoveryMetadata.ListenUris.ToString());
            Console.WriteLine("Received an online announcement from {0}", e.EndpointDiscoveryMetadata.Version);

        }

        static void OnOfflineEvent(object sender, AnnouncementEventArgs e)
        {
            Console.WriteLine("Received an offline announcement from {0}",
        e.EndpointDiscoveryMetadata.Address);
            Console.WriteLine("Received an offline announcement from {0}",
        e.EndpointDiscoveryMetadata.ContractTypeNames.ToString());
            Console.WriteLine("Received an offline announcement from {0}", e.EndpointDiscoveryMetadata.ListenUris.ToString());
            Console.WriteLine("Received an offline announcement from {0}", e.EndpointDiscoveryMetadata.Version);

        }
    }
}

The Output of the Application looks like this:

Announcement0.png

I hope this article is useful for you.


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.