Esempio n. 1
0
        static void Main(string[] args)
        {
            const string persistenceConnectionString = "Initial Catalog=SqlPersistenceService;Data Source=localhost;Integrated Security=SSPI;";
            const string transactionServiceDataBase  = "Initial Catalog=TransactionServiceSampleDB;Data Source=localhost;Integrated Security=SSPI;Enlist=false;";
            bool         validInteger   = false;
            Int32        transferAmount = 0;

            //Display account balances before requesting transfer amount
            QueryAccountService queryAccounts = new QueryAccountService(transactionServiceDataBase);

            Int32[] accountBalances = queryAccounts.QueryAccount(1);
            Console.WriteLine("The account balances for account number {0} are:  Checking : {1:c} , Savings : {2:c}",
                              1, accountBalances[0], accountBalances[1]);

            Console.WriteLine("Please enter an amount to transfer from Savings to Checking");

            while (!validInteger)
            {
                try
                {
                    transferAmount = Convert.ToInt32(Console.ReadLine());
                    if (transferAmount < 0)
                    {
                        Console.WriteLine("Please enter an amount greater than zero.");
                    }
                    else
                    {
                        validInteger = true;
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Please enter a valid amount and try again.");
                }
            }

            //Initiate the workflow run time
            using (WorkflowRuntime runtime = new WorkflowRuntime())
            {
                // Add the SQL persistence service
                SqlWorkflowPersistenceService persistenceService = new SqlWorkflowPersistenceService(persistenceConnectionString);
                runtime.AddService(persistenceService);

                // Add the query account service. This will be used to query the account balances
                runtime.AddService(queryAccounts);

                // Add the transactional service. This is the service which
                // does the work of crediting and debiting the amounts
                // This service participates in the work batch of the workflow instance
                TransactionalService transactionService = new TransactionalService(transactionServiceDataBase);
                runtime.AddService(transactionService);
                runtime.WorkflowCompleted  += new EventHandler <WorkflowCompletedEventArgs>(wr_OnWorkflowCompleted);
                runtime.WorkflowAborted    += new EventHandler <WorkflowEventArgs>(wr_OnWorkflowAborted);
                runtime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };
                runtime.StartRuntime();
                Dictionary <string, object> parameters = new Dictionary <string, object>();
                parameters.Add("TransferAmount", transferAmount);

                // Initiate the workflow
                runtime.CreateWorkflow(typeof(BalanceTransferWorkflow), parameters).Start();
                Console.WriteLine("Running the workflow");

                // Wait for the workflow to finish
                waitHandle.WaitOne();

                Console.WriteLine("Done running the workflow.");

                runtime.StopRuntime();
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {

            const string persistenceConnectionString = "Initial Catalog=SqlPersistenceService;Data Source=localhost;Integrated Security=SSPI;";
            const string transactionServiceDataBase = "Initial Catalog=TransactionServiceSampleDB;Data Source=localhost;Integrated Security=SSPI;Enlist=false;";
            bool validInteger = false;
            Int32 transferAmount = 0;

            //Display account balances before requesting transfer amount
            QueryAccountService queryAccounts = new QueryAccountService(transactionServiceDataBase);
            Int32[] accountBalances = queryAccounts.QueryAccount(1);
            Console.WriteLine("The account balances for account number {0} are:  Checking : {1:c} , Savings : {2:c}",
                 1, accountBalances[0], accountBalances[1]);

            Console.WriteLine("Please enter an amount to transfer from Savings to Checking");

            while (!validInteger)
            {
                try
                {
                    transferAmount = Convert.ToInt32(Console.ReadLine());
                    if (transferAmount < 0)
                    {
                        Console.WriteLine("Please enter an amount greater than zero.");
                    }
                    else
                    {
                        validInteger = true;
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Please enter a valid amount and try again.");
                }
            }

            //Initiate the workflow run time
            using (WorkflowRuntime runtime = new WorkflowRuntime())
            {
                // Add the SQL persistence service
                SqlWorkflowPersistenceService persistenceService = new SqlWorkflowPersistenceService(persistenceConnectionString);
                runtime.AddService(persistenceService);

                // Add the query account service. This will be used to query the account balances
                runtime.AddService(queryAccounts);

                // Add the transactional service. This is the service which 
                // does the work of crediting and debiting the amounts
                // This service participates in the work batch of the workflow instance
                TransactionalService transactionService = new TransactionalService(transactionServiceDataBase);
                runtime.AddService(transactionService);
                runtime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(wr_OnWorkflowCompleted);
                runtime.WorkflowAborted += new EventHandler<WorkflowEventArgs>(wr_OnWorkflowAborted);
                runtime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };
                runtime.StartRuntime();
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("TransferAmount", transferAmount);

                // Initiate the workflow
                runtime.CreateWorkflow(typeof(BalanceTransferWorkflow), parameters).Start();
                Console.WriteLine("Running the workflow");

                // Wait for the workflow to finish
                waitHandle.WaitOne();

                Console.WriteLine("Done running the workflow.");

                runtime.StopRuntime();
            }
        }