Click here to Skip to main content
15,897,187 members
Articles / Desktop Programming / Windows Forms

Integrating WCF Services

Rate me:
Please Sign up or sign in to vote.
4.84/5 (12 votes)
24 Apr 2008CPOL3 min read 44.9K   483   46  
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.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Integrator.Framework.ServiceManagerModel;

namespace Integrator.Framework.ServiceManagerModel
{
    public enum StatusEnum
    {
        Ready,
        Started,
        Stopped,
        Pause,
        Done
    }

    public class Operation
    {
        private Parameter[] input = null;
        private Parameter output = null;
        private FastInvoker fastInvoker = null;
        private StatusEnum state = StatusEnum.Ready;

        private Operation()
        {
        }

        private Operation(FastInvoker fastInvoker)
        {
            this.fastInvoker = fastInvoker;
        }

        public static Operation CreateOperation(Object hostObject, String methodName)
        {
            if (hostObject == null)
            {
                throw new ArgumentNullException("Invalid Host Object");
            }

            if (methodName == string.Empty)
            {
                throw new ArgumentNullException("Invalid Method Name");
            }

            FastInvoker invoker = new FastInvoker(hostObject, methodName);

            return new Operation(invoker);
        }        

        public void SetInput(Parameter param, int index)
        {
            if (index < 0 || index > input.Length - 1)
            {
                throw new IndexOutOfRangeException("Index out of range.");
            }

            this.input[index] = param;
        }

        public Parameter GetInput(int index)
        {
            if (index < 0 || index > input.Length - 1)
            {
                throw new IndexOutOfRangeException("Index out of range.");
            }

            return this.input[index];
        }

        public Parameter [] Input
        {
            get
            {
                return this.input;
            }
            set
            {
                this.input = value;
            }
        }

        public Parameter Output
        {
            get 
            {
                return this.output;
            }
            set
            {
                this.output = value;
            }
        }

        public void Run()
        {
            object returnObject = this.fastInvoker.Call(this.GetRawParameters());

            if (this.output != null)
            {
                this.output.ActualData = returnObject;
            }
        }

        public StatusEnum State
        {
            get
            {
                return this.state;
            }

            set
            {
                this.state = value;
            }
        }

        public string Name
        {
            get
            {
                return this.fastInvoker.theMethodInfo.Name;
            }
        }

        private object[] GetRawParameters()
        {
            object[] rawParameters = new object[this.Input.Length];

            for (int index = 0; index < this.Input.Length; index++)
            {
                rawParameters[index] = this.Input[index].ActualData;
            }

            return rawParameters;
        }

    }
}

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)
Singapore Singapore
Yes, I design. Then, I code. Next, I refactor.

Comments and Discussions