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

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.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.EventBroker;
using Integrator.Framework.ServiceManagerModel;
using Integrator.UI.Common;

namespace Integrator.UI.LoadedServices
{
    public partial class LoadedServicesView : UserControl
    {
        TreeNode rootNode = new TreeNode("Loaded Services");
        ContextMenu menu = null;

        [EventPublication(EventTopicConstants.ADD_OPERATION, PublicationScope.Global)]
        public event EventHandler<OperationEventArgs> AddOperationEvent;

        [EventPublication(EventTopicConstants.SHOW_PARAMETER, PublicationScope.Global)]
        public event EventHandler<ParameterEventArgs> ShowParameterEvent;

        public LoadedServicesView()
        {
            InitializeComponent();
            this.ConstructMenu();
        }

        private void ConstructMenu()
        {
            MenuItem item = new MenuItem("Add Operation");
            item.Click += new EventHandler(item_Click);
            MenuItem[] itemArray = new MenuItem[1];
            itemArray[0] = item;
            this.menu = new ContextMenu(itemArray);
        }

        void item_Click(object sender, EventArgs e)
        {
            if (this.AddOperationEvent != null)
            {
                this.AddOperationEvent(this, new OperationEventArgs((Operation)this.treeView.SelectedNode.Tag));
            }
        }

        public void AddServiceInstance(ServiceProxy proxy)
        {
            if (proxy == null)
            {
                throw new ArgumentNullException();
            }

            if (!this.treeView.Nodes.Contains(this.rootNode))
            {
                this.rootNode.Name = "Services";
                this.rootNode.SelectedImageKey = "Services";
                this.rootNode.ImageKey = "Services";
                this.treeView.Nodes.Add(this.rootNode);
            }

            TreeNode node = new TreeNode(proxy.Name);
            node.Name = proxy.ToString();
            node.Tag = proxy;
            node.ImageKey = "Service";

            this.rootNode.Nodes.Add(node);

            this.AddOperationNode(node, proxy);
        }

        private void AddOperationNode(TreeNode node, ServiceProxy proxy)
        {
            IList operations = proxy.Operations;
            foreach (Operation operation in operations)
            {
                TreeNode childNode = new TreeNode(operation.Name);
                childNode.Name = operation.Name;
                childNode.Tag = operation;
                childNode.SelectedImageKey = "Operation";
                childNode.ImageKey = "Operation";

                node.Nodes.Add(childNode);

                this.AddParameterNode(childNode, operation);

            }
        }

        private void AddParameterNode(TreeNode node, Operation operation)
        {
            //Add Input Parameters first
            for (int index=0; index<operation.Input.Length; index++)
            {
                Parameter input = operation.Input[index];

                if (input != null)
                {
                    TreeNode childNode = new TreeNode(input.Key);
                    childNode.Name = input.Key;
                    childNode.Tag = input;
                    childNode.SelectedImageKey = "InputParameter";
                    childNode.ImageKey = "InputParameter";

                    node.Nodes.Add(childNode);
                }
            }

            //Add the output parmeter
            if (operation.Output != null)
            {
                TreeNode childNode = new TreeNode("Output");
                childNode.Name = "Output";
                childNode.Tag = operation.Output;
                childNode.SelectedImageKey = "OutputParameter";
                childNode.ImageKey = "OutputParameter";

                node.Nodes.Add(childNode);
            }
        }

        private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            treeView.SelectedNode = e.Node;

            if (e.Button == MouseButtons.Right && e.Node.SelectedImageKey == "Operation")
            {
                this.menu.Show(this.treeView, new Point(e.X, e.Y));
            }
            else if (e.Button == MouseButtons.Left)
            {
                if (this.treeView.SelectedNode.Tag != null)
                {
                    bool isParameterNode = this.treeView.SelectedNode.Tag is Parameter;

                    if (isParameterNode)
                    {
                        if (this.ShowParameterEvent != null)
                        {
                            this.ShowParameterEvent(this, new ParameterEventArgs((Parameter)this.treeView.SelectedNode.Tag));
                        }
                    }
                }
            }
        }        
    }
}

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
Web04 | 2.8.140916.1 | Last Updated 25 Apr 2008
Article Copyright 2008 by Ben Liew
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid