Click here to Skip to main content
15,886,258 members
Articles / Hosted Services / Azure

Automating Windows Applications Using Azure AppFabric Service Bus

Rate me:
Please Sign up or sign in to vote.
4.93/5 (38 votes)
13 Nov 2010CPOL8 min read 46.9K   1.3K   49  
Unmanaged Windows GUI application with no exported program interface gets automated with injected code. A WCF aware injected component allows world-wide located clients to communicate with automated application via Azure AppFabric Service Bus despite firewalls and dynamic IP addresses
using System;
using System.Collections.Generic;
using System.Text;
using SvcHost;

namespace NotepadHandlerNET
{
    /// <summary>
    /// Class NotificationBase serves as base for all notification classes.
    /// </summary>
    public class NotificationBase
    {
        #region NotifAddressTable Helper Class
        /// <summary>
        /// Class NotifAddressTable contains all subscribers (clients) accessed by their URIs.
        /// </summary>
        public class NotifAddressTable : Dictionary<string, INotification>
        {
        }
        #endregion

        #region Variables
        protected static NotifAddressTable nat = null;
        protected Exception e = null;
        #endregion

        #region Static Ctor
        /// <summary>
        /// Static ctor.
        /// </summary>
        static NotificationBase()
        {
            nat = new NotifAddressTable();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Method returns notification proxy for a given endpoint URI.
        /// </summary>
        private INotification FindNotificationClient(string endpointAddressUriNotification)
        {
            INotification notifProxy = null;
            if (null != endpointAddressUriNotification)
            {
                try
                {
                    notifProxy = nat[endpointAddressUriNotification.ToLower()];
                }
                catch //(Exception e)
                {
                }
            }

            return notifProxy;
        }

        /// <summary>
        /// Method returns notification proxy of a given endpoint URI.
        /// </summary>
        protected void RemoveNotificationClient(INotification notifProxy)
        {
            lock (typeof(NotifAddressTable))
            {
                string uriToRemove = null;
                foreach (string uri in nat.Keys)
                    if (nat[uri] == notifProxy)
                        uriToRemove = uri;

                nat.Remove(uriToRemove);        
            }
        }

        /// <summary>
        /// Wrapper method around notification call.
        /// </summary>
        protected object Notify(INotification notifProxy, string sender, string eventName, object data)
        {
            object ret = null;
            if (null != notifProxy)
            {
                try
                {
                    ret = notifProxy.Notify(sender, eventName, data);
                }
                catch //(Exception e)
                {
                    RemoveNotificationClient(notifProxy);
                }
            }

            return ret;
        }

        /// <summary>
        /// Method provides (either finds or creates) notification proxy. Synchronization is required.
        /// </summary>
        protected INotification GetNotificationClient(string endpointAddressUriNotification)
        {
            INotification notifProxy = null;
            string uri = endpointAddressUriNotification.ToLower();

            lock (typeof(NotifAddressTable))
            {
                notifProxy = FindNotificationClient(endpointAddressUriNotification);
                if (null == notifProxy)
                {
                    try
                    {
                        notifProxy = Client.ConnectTo<INotification>(endpointAddressUriNotification);
                        if (null != notifProxy)
                            nat.Add(uri, notifProxy);
                    }
                    catch (Exception e)
                    {
                        this.e = e;
                    }
                }
            }

            return notifProxy;
        }

        /// <summary>
        /// Method returns a list of all notification proxies. Synchronization is required.
        /// </summary>
        protected List<INotification> GetAllNotificationSubscribers()
        {
            List<INotification> listProxy = new List<INotification>();

            lock (typeof(NotifAddressTable))
            {
                foreach (string uri in nat.Keys)
                    listProxy.Add(FindNotificationClient(uri));
            }

            return listProxy;
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel


  • Nov 2010: Code Project Contests - Windows Azure Apps - Winner
  • Feb 2011: Code Project Contests - Windows Azure Apps - Grand Prize Winner



Comments and Discussions