Click here to Skip to main content
15,897,891 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 47K   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 System.ServiceModel;
using System.Reflection;
using System.Runtime.Remoting;

namespace NotepadHandlerNET
{
    #region ICommander ServiceContract
    /// <summary>
    /// ServiceContract interface for embedded to Notepad component.
    /// </summary>
    [ServiceContract]
    public interface ICommander
    {
        [OperationContract]
        object Command(string sender, string commandName, object commandData);
    }
    #endregion

    #region Commander Service Class
    /// <summary>
    /// Service class implementing ServiceContract.
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
    public class Commander : ICommander
    {
        static readonly private Type[] types = null;

        /// <summary>
        /// Static ctor.
        /// </summary>
        static Commander()
        {
            types = Assembly.GetExecutingAssembly().GetTypes();
        }

        /// <summary>
        /// Command method is called by client. It creates actual command class by the command name and
        /// calls its DoCommand method.
        /// </summary>
        public object Command(string sender, string commandName, object commandData)
        {
            object ret = null;
            try
            {
                ICommandImpl comImpl = Activator.CreateInstance(GetCommandTypeByName(commandName)) as ICommandImpl;
                if (null != comImpl)
                    ret = comImpl.DoCommand(sender, commandData);
            }
            catch (Exception e)
            {
                ret = e;
            }

            return ret;
        }

        /// <summary>
        /// A helper method getting actual command type using the command name.
        /// </summary>
        private Type GetCommandTypeByName(string commandName)
        {
            Type commandType = null;
            foreach (Type type in types)
                if (type.Name.ToLower() == commandName.ToLower())
                {
                    try
                    {
                        commandType = type;
                        break;
                    }
                    catch
                    {
                    }
                }

            return commandType;
        }
    }
    #endregion

    #region Commnad Implementation Interface
    /// <summary>
    /// Interface to be implemented by all commands.
    /// </summary>
    public interface ICommandImpl
    {
        object DoCommand(string sender, object commandData);
    }
    #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