Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Integrating WCF Services

, 24 Apr 2008
This article describes how WCF services can be loaded on-the-fly (without prior knowledge of the services’ contracts), setting its parameters and sequencing their invocation.
integrator.zip
Integrator
Debug
Integrator.Common
bin
Distribution
Release
Properties
Integrator.DynamicProxyModel
bin
Debug
Properties
Integrator.Module.LoadedServices
bin
Debug
Properties
Integrator.Module.Sequencer
bin
Debug
Properties
Resources
Done.bmp
Paused.bmp
Ready.bmp
Started.bmp
Stopped.bmp
Integrator.Sequencer
bin
Debug
Properties
Integrator.ServiceManagerModel
bin
Debug
Parameter
Properties
Integrator.Shell
bin
Debug
Distribution
Release
Properties
Settings.settings
Integrator.suo
Integrator.TestServer
bin
Debug
Properties
Integrator.UI.ParameterEditor
bin
Debug
Properties
Release
Resources
Done.bmp
In.ico
Microsoft.Practices.CompositeUI.dll
Microsoft.Practices.CompositeUI.WinForms.dll
Microsoft.Practices.ObjectBuilder.dll
Operation.ico
Out.ico
Paused.bmp
Ready.bmp
Service.ico
Services.ico
Start.bmp
Started.bmp
Stopped.bmp
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.ServiceModel;
using System.Text.RegularExpressions;
using Integrator.Framework.DynamicProxyModel;

namespace Integrator.Framework.ServiceManagerModel
{
    public class ServiceProxy
    {
        private OperationList m_OperationList = null;
        private string m_Name = string.Empty;
        private static int m_OutputKey = 0;

        public ServiceProxy(DynamicProxy proxy, string contractName)
        {
            if (proxy == null)
            {
                throw new ArgumentNullException("Invalid proxy given");
            }
            
            this.m_Name = proxy.ObjectType.ToString();

            this.m_OperationList = new OperationList();
            
            MethodInfo[] methodInfos = proxy.ProxyType.GetInterface(contractName).GetMethods();

            foreach (MethodInfo info in methodInfos)
            {
                Operation operation = Operation.CreateOperation(proxy.ObjectInstance, info.Name);
                operation.Input = this.RetrieveParameters(info);

                Parameter outputParameter = this.CreateReturnParameter(info.ReturnParameter.ParameterType);

                if (outputParameter != null)
                {
                    operation.Output = outputParameter;
                }
                this.m_OperationList.Add(operation);                
            }
        }

        public OperationList Operations
        {
            get { return this.m_OperationList; }
        }

        public string Name
        {
            get { return this.m_Name; }
        }

        private Parameter [] RetrieveParameters(MethodInfo info)
        {
            ParameterInfo [] parameterInfos = info.GetParameters();
            Parameter[] parameters = new Parameter[parameterInfos.Length];

            for (int index=0; index<parameterInfos.Length; index++)
            {
                ParameterInfo parameterInfo = parameterInfos[index];
                Type parameterType = parameterInfo.ParameterType;

                parameters[index] = this.CreateParameter(parameterType, parameterInfo.Name);
            }

            return parameters;
        }

        private Parameter CreateReturnParameter(Type parameterType)
        {
            string key = m_OutputKey.ToString();
            m_OutputKey++;
            return this.CreateParameter(parameterType, key);
        }

        private Parameter CreateParameter(Type parameterType, string key)
        {
            Parameter parameter = null; 

            if (parameterType.Name == "Int64")
            {
                parameter = ParameterFactory.CreateLong(key);
            }
            else if (parameterType.Name == "Int32")
            {
                parameter = ParameterFactory.CreateInteger(key);
            }
            else if (parameterType.Name == "Int16")
            {
                parameter = ParameterFactory.CreateShort(key);
            }
            else if (parameterType.Name == "String")
            {
                parameter = ParameterFactory.CreateString(key);
            }
            else if (parameterType.Name == "Double")
            {
                parameter = ParameterFactory.CreateDouble(key);
            }
            else if (parameterType.Name == "Single")
            {
                parameter = ParameterFactory.CreateFloat(key);
            }
            else if (parameterType.Name == "Boolean")
            {
                parameter = ParameterFactory.CreateBoolean(key);
            }
            else if (parameterType.Name == "Void")
            {
                // Do nothing
            }
            else
            {
                // For Complex Type
                parameter = ParameterFactory.CreateComplexParameter(key);

                ComplexParameter complexParameter = (ComplexParameter)parameter;
                FieldInfo[] fieldInfos = parameterType.GetFields();

                foreach (FieldInfo info in fieldInfos)
                {
                    complexParameter.AddParameter(CreateParameter(info.FieldType, info.Name));
                }
            }

            return parameter;
        }
    }
}

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)

Share

About the Author

Ben Liew
Software Developer (Senior)
Singapore Singapore
Yes, I design. Then, I code. Next, I refactor.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 25 Apr 2008
Article Copyright 2008 by Ben Liew
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid