Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#

Automating Windows Applications Using the WCF Equipped Injected Component

Rate me:
Please Sign up or sign in to vote.
4.96/5 (47 votes)
15 Nov 2010CPOL14 min read 102.8K   2K   122  
A .NET WCF equipped component injected into automated process allows both local and remote clients to control the process and get its asynchronous events.
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace SvcHost
{
    /// <summary>
    /// Client class provides proxy for server connection.
    /// </summary>
    public class Client
    {
        /// <summary>
        /// Method creates proxy by its endpoint URI resolving server metadata.
        /// </summary>
        public static IT ConnectTo<IT>(string endpointAddressUri) where IT : class
        {
            IT proxy = null;
            string err = null;
            ServiceEndpointCollection endpoints = null;

            string realAddress = ExtractAddress(endpointAddressUri); //Igor

            try
            {
                //Igor
                endpoints = MetadataResolver.Resolve(typeof(IT), 
                    new EndpointAddress(GetProperMexEndpointAddress(endpointAddressUri)));
            }
            catch (Exception e)
            {
                err = e.Message;
            }

            if (null == err)
                foreach (ServiceEndpoint endpoint in endpoints)
                {
                    try
                    {
                        proxy = new ChannelFactory<IT>(endpoint.Binding, 
                            new EndpointAddress(endpoint.Address.Uri.AbsoluteUri.Replace("localhost", realAddress)))
                            .CreateChannel(); //Igor
                        break;
                    }
                    catch
                    {
                    }
                }

            return proxy;
        }

        //Igor
        private static string GetProperMexEndpointAddress(string uri)
        {
            return string.Format("{0}{1}Mex", uri, uri[uri.Length - 1] == '/' ? string.Empty : "/");
        }

        //Igor
        private static string ExtractAddress(string uri)
        {
            return uri.Split(new char[] { ':' })[1].Replace("//", string.Empty);
        }
    }
}

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