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

Host and Workflow: Two Worlds to Communicate - Part II

Rate me:
Please Sign up or sign in to vote.
4.83/5 (15 votes)
3 Oct 2008CPOL8 min read 52.2K   394   21  
Part II: Inter-communications: Workflow -> Host through the CallExternalMethod Activity.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;

namespace CommunicationCallExternalMethod
{
    class Program
    {
        //You can pass the arguments to Workflow as line parameters, but you dont need to do.
        //You can used fixed value, or take from a database or form elsewhere you need them!
        //Here we take them optionally from command line, but you can use the defaults. 
        static string _filepath = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\ieuinit.inf";
        static int _readQuantity = 20;
        
        /// <summary>
        /// Command Options....
        /// programname ["filepath"] [character quantity]
        /// filepath is a string sorrounding by " and the second parameter is a integer number.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            
            if (args.Length > 0)
            {
                if (System.IO.File.Exists(args[0].ToString()))
                { _filepath = args[0].ToString(); }
            }
            if (args.Length > 1)
            {
                int.TryParse(args[1].ToString(), out _readQuantity);
            }


            using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                AutoResetEvent waitHandle = new AutoResetEvent(false);
                workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { waitHandle.Set(); };
                workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };
                
                //Add support to the Communication Service........................
                
                //Declare a ExternalDataExchangeService class
                ExternalDataExchangeService dataservice = new ExternalDataExchangeService();
                //Add to workflow runtime
                workflowRuntime.AddService(dataservice);
                //Declare our CommunicationService instance
                CommunicationService cservice = new CommunicationService();
                //Add to the ExternalDataService
                dataservice.AddService(cservice);
                //Add a handler to Service Event
                cservice.SendDataToHostEvent += new EventHandler<SendDataToHostEventArgs>(cservice_SendDataToHostEvent);
                
                // end of support to Communication Service.........................
                
                //Create a dictionary to pass parameter to workflow....
                Dictionary<string, object> argumentDictionary = new Dictionary<string, object>();
                //Pass the first parameter: the file path. You must use the same name that was defined as variable in the workflow.
                argumentDictionary.Add("FilePath", _filepath);
                argumentDictionary.Add("Quantity", _readQuantity);

                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(CommunicationCallExternalMethod.Workflow1), argumentDictionary);
                instance.Start();

                waitHandle.WaitOne();
            }
        }

        static void cservice_SendDataToHostEvent(object sender, SendDataToHostEventArgs e)
        {
            Console.WriteLine("Returned Information from workflow............");
            Console.WriteLine("The firt {0} characteres from file {1} are: \r\n {2}",
                                _readQuantity.ToString(), _filepath, e.Response);
            Console.ReadLine();                    
           
        }
    }
}

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) Avalon Development
United States United States
Jose A. Garcia Guirado, Electronic Engineer, graduated in Havana/Cuba 1982, MCTS, MCSD.NET, MCAD.NET, MCSE. Worked in the Institute for Cybernetics and Mathematics of Academy of Science of Cuba for 8 years; since 1995 working as free software architect, developer and adviser, first in Argentina and from 2003 to 2010, in Germany as External consultant in DWS Luxembourg, AIXTRON AG and Shell Deutschland GmbH and from 2010 to 2012 in Mexico working for Twenty Century Fox, and Mexico Stock Exchange (BMV). From 2013 to now in USA, Florida, First in FAME Inc. and now as Senior Software Engineer in Spirit Airlines.

Comments and Discussions