Click here to Skip to main content
15,892,537 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.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)


Written By
Software Developer (Senior)
Singapore Singapore
Yes, I design. Then, I code. Next, I refactor.

Comments and Discussions