Ask a questionAsk a question
 

AnswerWorkflow terminated unexpectedly

  • Thursday, October 29, 2009 7:59 PMmilan82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi.

    I'm using Windows Workflow Foundation in a Win Form application . I'm building application for approving Bank credit requests .
    I have two workflows in my app where first invoking the second. Both of them are State Machine workflows .
    I think i have the same problem in two places in my application .
    First place(first workflow) : When I first time open the form to fill credit reguest everything works fine and when i click on the save button and first workflow is persisted,but when i reopen the form,fill the credit request and click the save button my first worflow terminates unexpectedly .
    Second place(second workflow) : I have a task list with credit requests .When you double-click on the row in a table,details form will be opened and will be filled with selected row data .First time is no problem,i click  on the save button ,both worflows are persisted and details form is closed.But,when i back to the task list and double-click on the other item on the task list and open details form again, when i click save button,second workflow will be terminated .

    When  I exit from my application and run it again everything works fine .
    My app needs to be reopenned or the workflow terminates for any reason .

    Did any of you had similar problems?

Answers

  • Sunday, November 01, 2009 10:51 AMmilan82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The form  for which i posted the code was not my  application main form code . So, every time when i was loading this form,i was calling this method:


                this.StartWorkflowRuntime();

    ,and this method have this line :


                _workflowRuntime = new WorkflowRuntime();

    I think that you understand now what was the problem :) I was looking the solution in the wrong place .

    I will post only the code related to WWF  .


    First, the WorkflowFactory class :

    public static class WorkflowFactory
        {

             // Singleton instance of the workflow runtime
                private static WorkflowRuntime _workflowRuntime = null;
                //BankJobDataConnector service;

                // Lock (sync) object
                private static object _syncRoot = new object();


                private static AutoResetEvent _waitHandle = new AutoResetEvent(false);

                // Factory method
                public static WorkflowRuntime GetWorkflowRuntime()
                {
                    // Lock execution thread in case of multi-threaded
                    // (concurrent) access.
                    lock (_syncRoot)
                    {
                        // Check for startup condition
                        if (null == _workflowRuntime)
                        {
                            // Provide for shutdown
                            AppDomain.CurrentDomain.ProcessExit += new EventHandler(StopWorkflowRuntime);
                            AppDomain.CurrentDomain.DomainUnload += new EventHandler(StopWorkflowRuntime);

                            // Not started, so create instance
                           
                            //_workflowRuntime.WorkflowIdled += new EventHandler<WorkflowEventArgs>(wr_WorkflowIdled);

                            // Add the external data service
                            _workflowRuntime = new WorkflowRuntime();

                        
                            SqlWorkflowPersistenceService stateService = new SqlWorkflowPersistenceService(
                                PersistanceServiceSettings.ConnectionString);
                            _workflowRuntime.AddService(stateService);

                            SqlTrackingService trackingService = new SqlTrackingService(PersistanceServiceSettings.ConnectionString);
                            _workflowRuntime.AddService(trackingService);

                            SharedConnectionWorkflowCommitWorkBatchService sharedService = new  SharedConnectionWorkflowCommitWorkBatchService(PersistanceServiceSettings.ConnectionString);
                            _workflowRuntime.AddService(sharedService);
                            // Start the Workflow services
                            _workflowRuntime.StartRuntime();
                        } // if

                        // Return singleton instance
                        return _workflowRuntime;
                    } // lock
                }

                // Shutdown method
                static void StopWorkflowRuntime(object sender, EventArgs e)
                {
                    if (_workflowRuntime != null)
                    {
                        if (_workflowRuntime.IsStarted)
                        {
                            try
                            {
                                // Stop the runtime
                                _workflowRuntime.StopRuntime();
                            }
                            catch (ObjectDisposedException)
                            {
                                // Already disposed of, so ignore...
                            } // catch
                        } // if
                    } // if
                }
               
            }

    Second,the form class(only the part related to to WWF) :

            WorkflowBankJobDataService _dataService;
            WorkflowRuntime _workflowRuntime;
            WorkflowInstance _instance;
           
            BankJobDataConnector service;
            private Dictionary<string, StateMachineWorkflowInstance> stateMachineInstances;


     


     private Guid SubmitToWorkflow()
            {

                Int32 brojZahteva = Convert.ToInt32(this.zK_BROJTextBox.Text);
                string statusZahteva = this.zK_BROJTextBox.Text;
              
              
               
                // Fill the Parameters collection for this instance of the workflow
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("IDZahteva", brojZahteva);
                parameters.Add("StatusZahteva", statusZahteva);
          

             

                // Start the workflow instance
                _instance = this._workflowRuntime.CreateWorkflow(typeof(BankJobWorkflow.BankJobWorkflow), parameters);

                StateMachineWorkflowInstance stateMachineInstance = new StateMachineWorkflowInstance(_workflowRuntime, _instance.InstanceId);

                _dataService = BankJobDataService.WorkflowBankJobDataService.CreateDataService(_instance.InstanceId, _workflowRuntime);
                _dataService.SubvencijaUpdated += new EventHandler<SubvencijaEventArgs>(dataConnectorService_SubvencijaUpdated);

                _instance.Start();
             

                // Add the StateMachineInstance object for our Workflow to our dictionary
                this.stateMachineInstances.Add(_instance.InstanceId.ToString(), stateMachineInstance);

                // Return the WorkflowInstanceId
                return _instance.InstanceId;
            }

    void dataConnectorService_SubvencijaUpdated(object sender, BankJobDataService.SubvencijaEventArgs e)
            {
                IAsyncResult result = this.BeginInvoke(
                    new EventHandler(
                           delegate
                           {
                               this.zK_SUBVENCIJAMaskedTextBox.Text = (e.Subvencija).ToString();
                              
                           } // delegate
                        ), null, null
                ); // BeginInvoke
                this.EndInvoke(result);
            }

    void wr_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)
            {

               
                MessageBox.Show(string.Format("Workflow je terminiran, sa workflow instancom={0}", e.Exception.Message), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
               
              
           }

            void wr_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
            {
               
                MessageBox.Show(string.Format("Workflow je kompletiran, sa workflow instancom={0}", e.WorkflowInstance.InstanceId), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
               
            }


    private void frmZahtevZaKredit_Load(object sender, EventArgs e)
            {
              

               
                _workflowRuntime = WorkflowFactory.GetWorkflowRuntime();

                _workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(wr_WorkflowCompleted);
                _workflowRuntime.WorkflowTerminated += new EventHandler<WorkflowTerminatedEventArgs>(wr_WorkflowTerminated);
              
                // Create a new dictionary to store the StateMachineInstance objects
                this.stateMachineInstances = new Dictionary<string, StateMachineWorkflowInstance>();

              
              
              
            }

    private void btnSacuvaj_Click(object sender, EventArgs e)
            {
                int brojZahteva = Convert.ToInt32(this.zK_BROJTextBox.Text);
                string status = this.zK_STATUSComboBox.Text;
                string subvencijaZahteva = this.zK_SUBVENCIJAMaskedTextBox.Text;
                double iznosSubvencije;
                double subvencijaOpciono;
             
               

                if (status != "U OBRADI")
                {
                    MessageBox.Show("Odabrani zahtev je obradjen!");
                }
                if (status == "U OBRADI")
                {
                    if (this.zK_BROJTextBox.Text != "")
                    {



                        Guid workflowInstanceId =
                        this.SubmitToWorkflow();



                        this.zK_INSTANCA_WFTextBox.Text = workflowInstanceId.ToString();
                        service = (BankJobDataConnector)_workflowRuntime.GetService(typeof(BankJobDataConnector));
                        service.RaiseStatusUObradi(workflowInstanceId, brojZahteva, status, subvencijaZahteva);
                    }

                    frmSubvencija subvencija = new frmSubvencija();

                    subvencija.ShowDialog();
                    if (subvencija.DialogResult == DialogResult.Cancel)
                    {
                       
                        try{
                        this.kR_ANUITETMaskedTextBox.Text = ((Double.Parse(this.kR_IZNOSTextBox.Text) * ((Math.Pow(1 + Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) / 1200,
                    Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) * Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) / 1200 / (((Math.Pow(1 + Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) / 1200,
                    Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) - 1))))))).ToString();
                        }
                        catch (FormatException ex)
                        {
                            string str = "Morate popuniti sva polja!";
                            FormatException format = new FormatException(str, ex);
                            format.Source = this.Text;
                            ExceptionMessageBox box = new ExceptionMessageBox(format);
                            box.Show(this);
                        }

                        //this.zK_SUBVENCIJAMaskedTextBox.Text = "0.0";
                        try
                        {
                            _dataService.RaiseSubvencijaUpdatedEvent(0.0d);
                        }
                        catch (InvalidOperationException ex)
                        {
                            string str = "IZABRANI WORKFLOW VISE NIJE PRISUTAN U BAZI";
                            InvalidOperationException format = new InvalidOperationException(str, ex);
                            format.Source = this.Text;
                            ExceptionMessageBox box = new ExceptionMessageBox(format);
                            box.Show(this);
                        }


                    }

                    if (subvencija.DialogResult == DialogResult.OK)
                    {
      
                        if (this.ckbSubvencijaOpciono.Checked == false)
                        {
                            iznosSubvencije = 0.1;
                            try{
                            this.kR_ANUITETMaskedTextBox.Text = ((Double.Parse(this.kR_IZNOSTextBox.Text) * ((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) * (Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200 / (((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) - 1))))))).ToString();
                            }
                            catch (FormatException ex)
                            {
                                string str = "Morate popuniti sva polja!";
                                FormatException format = new FormatException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }
                        }
                        if (this.ckbSubvencijaOpciono.Checked == true)
                        {
                            subvencijaOpciono = Convert.ToDouble(this.mtbIznosSubvencije.Text) / 10;
                            iznosSubvencije = subvencijaOpciono;

                            try{
                            this.kR_ANUITETMaskedTextBox.Text = ((Double.Parse(this.kR_IZNOSTextBox.Text) * ((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) * (Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200 / (((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) - 1))))))).ToString();
                            }
                            catch (FormatException ex)
                            {
                                string str = "Morate popuniti sva polja!";
                                FormatException format = new FormatException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }
                        }



                        if (this.ckbSubvencijaOpciono.Checked == false)
                        {
                            //this.zK_SUBVENCIJAMaskedTextBox.Text = "0.1";
                            try
                            {
                                _dataService.RaiseSubvencijaUpdatedEvent(0.1d);
                            }
                            catch (InvalidOperationException ex)
                            {
                                string str = "IZABRANI WORKFLOW VISE NIJE PRISUTAN U BAZI";
                                InvalidOperationException format = new InvalidOperationException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }

                        }
                        if (this.ckbSubvencijaOpciono.Checked == true)
                        {
                            subvencijaOpciono = Convert.ToDouble(this.mtbIznosSubvencije.Text) / 10;
                            try
                            {
                                _dataService.RaiseSubvencijaUpdatedEvent(subvencijaOpciono);
                            }
                            catch (InvalidOperationException ex)
                            {
                                string str = "IZABRANI WORKFLOW VISE NIJE PRISUTAN U BAZI";
                                InvalidOperationException format = new InvalidOperationException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }
                        }


                    }





                    try
                    {
                        this.zK_UKUPAN_IZNOSTextBox.Text = (Double.Parse(this.kR_ANUITETMaskedTextBox.Text) * Double.Parse(this.kR_ROK_OTPLATETextBox.Text)).ToString();
                        this.kR_PROTIVVREDNOSTTextBox.Text = (Decimal.Parse(this.kR_IZNOSTextBox.Text) * Decimal.Parse(this.kUR_SREDNJIComboBox.Text)).ToString();
                        this.zK_KREDITNA_SPOSOBNOSTTextBox.Text = ((Double.Parse(this.zK_PRIMANJA_REDOVNATextBox.Text) + Double.Parse(this.zK_PRIMANJA_VANREDNATextBox.Text)) / 2).ToString();
                    }
                    catch (FormatException ex)
                    {
                        string str = "Morate popuniti sva polja!";
                        FormatException format = new FormatException(str, ex);
                        format.Source = this.Text;
                        ExceptionMessageBox box = new ExceptionMessageBox(format);
                        box.Show(this);
                    }
                    // _workflowRuntime.WorkflowIdled += new EventHandler<WorkflowEventArgs>(wr_WorkflowIdled);

                    this.Validate();
                    try
                    {
                        this.zAHTEV_ZA_KREDITBindingSource.EndEdit();
                    }
                    catch (NoNullAllowedException ex)
                    {
                        string str = "Morate popuniti sva polja!";
                        NoNullAllowedException format = new NoNullAllowedException(str, ex);
                        format.Source = this.Text;
                        ExceptionMessageBox box = new ExceptionMessageBox(format);
                        box.Show(this);
                    }
                    catch (ConstraintException ex)
                    {
                        string str = "Zahtev sa ovim brojem vec postoji u tabeli!Ne mozete uneti dva puta isti zahtev!!";
                        ConstraintException format = new ConstraintException(str, ex);
                        format.Source = this.Text;
                        ExceptionMessageBox box = new ExceptionMessageBox(format);
                        box.Show(this);
                    }
                    if (!System.Convert.ToBoolean(this.zAHTEV_ZA_KREDITTableAdapter.Update(this.bankJobDataSet.ZAHTEV_ZA_KREDIT) > 0))
                    {
                        MessageBox.Show("Neuspesan UPDATE.");
                    }

                    //  UnloadInstance(workflowInstanceId);
                    try
                    {
                        _instance.TryUnload();
                      
                        MessageBox.Show("Zahtev je prosledjen sluzbi za obradu zahteva");

                    } // try
                    catch (Exception ex)
                    {
                        MessageBox.Show(String.Format("Greska prilikom unloadovanja workflowa" +
                        " instance: '{0}'", ex.Message));
                    }
                }// catch
               
            }


    The second workflow had similar problems so i won't posting the code.

    Regards .





    • Marked As Answer bymilan82 Sunday, November 01, 2009 10:51 AM
    •  

All Replies

  • Friday, October 30, 2009 3:09 PMGeert van Horrik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    How do you retrieve the workflow instance when opening the forms? Can you show us that code? And are you sure the workflows are already persisted at the time, or only when you close the application?


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
  • Friday, October 30, 2009 4:07 PMmilan82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


            private void StartWorkflowRuntime()
            {

                _workflowRuntime = new WorkflowRuntime();
           
               
                _workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(wr_WorkflowCompleted);
                _workflowRuntime.WorkflowTerminated += new EventHandler<WorkflowTerminatedEventArgs>(wr_WorkflowTerminated);
               

                // Add the external data service

                ExternalDataExchangeService dataService = new ExternalDataExchangeService();
                _workflowRuntime.AddService(dataService);

                service = new BankJobDataConnector();
                dataService.AddService(service);

                bool unloadOnIdle = true;
                TimeSpan instanceOwnershipDuration = TimeSpan.MaxValue;
                TimeSpan loadingInterval = new TimeSpan(0, 0, 3);

                SqlWorkflowPersistenceService stateService = new SqlWorkflowPersistenceService(
                    PersistanceServiceSettings.ConnectionString, unloadOnIdle, instanceOwnershipDuration, loadingInterval);
                _workflowRuntime.AddService(stateService);

                SqlTrackingService trackingService = new SqlTrackingService(PersistanceServiceSettings.ConnectionString);
                _workflowRuntime.AddService(trackingService);
               
                SharedConnectionWorkflowCommitWorkBatchService sharedService = new SharedConnectionWorkflowCommitWorkBatchService(PersistanceServiceSettings.ConnectionString);
                _workflowRuntime.AddService(sharedService);
                // Start the Workflow services
                _workflowRuntime.StartRuntime();
               
            }

            private Guid SubmitToWorkflow()
            {

                Int32 brojZahteva = Convert.ToInt32(this.zK_BROJTextBox.Text);
                string statusZahteva = this.zK_BROJTextBox.Text;
            
              
               
             
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("IDZahteva", brojZahteva);
                parameters.Add("StatusZahteva", statusZahteva);
             

             

                // Start the workflow instance
                _instance = this._workflowRuntime.CreateWorkflow(typeof(BankJobWorkflow.BankJobWorkflow), parameters);

                StateMachineWorkflowInstance stateMachineInstance = new StateMachineWorkflowInstance(_workflowRuntime, _instance.InstanceId);

                _dataService = BankJobDataService.WorkflowBankJobDataService.CreateDataService(_instance.InstanceId, _workflowRuntime);
                _dataService.SubvencijaUpdated += new EventHandler<SubvencijaEventArgs>(dataConnectorService_SubvencijaUpdated);

                _instance.Start();
              

              
                this.stateMachineInstances.Add(_instance.InstanceId.ToString(), stateMachineInstance);

                // Return the WorkflowInstanceId
                return _instance.InstanceId;
            }

            void dataConnectorService_SubvencijaUpdated(object sender, BankJobDataService.SubvencijaEventArgs e)
            {
                IAsyncResult result = this.BeginInvoke(
                    new EventHandler(
                           delegate
                           {
                               this.zK_SUBVENCIJAMaskedTextBox.Text = (e.Subvencija).ToString();
                              
                           } // delegate
                        ), null, null
                ); // BeginInvoke
                this.EndInvoke(result);
            }

          

            void wr_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)
            {
                MessageBox.Show(string.Format("Workflow je terminiran, sa workflow instancom={0}", e.Exception.Message), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                _waitHandle.Set();
              
           }

            void wr_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
            {
                MessageBox.Show(string.Format("Workflow je kompletiran, sa workflow instancom={0}", e.WorkflowInstance.InstanceId), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                _waitHandle.Set();
            }

          
          
          

            private void frmZahtevZaKredit_Load(object sender, EventArgs e)
            {
              

               
                this.StartWorkflowRuntime();

                this.stateMachineInstances = new Dictionary<string, StateMachineWorkflowInstance>();

              
              
              
            }

          
          

            private void btnSave_Click(object sender, EventArgs e)
            {
                int brojZahteva = Convert.ToInt32(this.zK_BROJTextBox.Text);
                string status = this.zK_STATUSComboBox.Text;
                string subvencijaZahteva = this.zK_SUBVENCIJAMaskedTextBox.Text;
                double iznosSubvencije;
                double subvencijaOpciono;
              
               

                if (status != "U OBRADI")
                {
                    MessageBox.Show("Odabrani zahtev je obradjen!");
                }
                if (status == "U OBRADI")
                {
                    if (this.zK_BROJTextBox.Text != "")
                    {



                        Guid workflowInstanceId =
                        this.SubmitToWorkflow();



                        this.zK_INSTANCA_WFTextBox.Text = workflowInstanceId.ToString();
                        service.RaiseStatusUObradi(workflowInstanceId, brojZahteva, status, subvencijaZahteva);
                    }

                    frmSubvencija subvencija = new frmSubvencija();

                    subvencija.ShowDialog();
                    if (subvencija.DialogResult == DialogResult.Cancel)
                    {

                        try{
                        this.kR_ANUITETMaskedTextBox.Text = ((Double.Parse(this.kR_IZNOSTextBox.Text) * ((Math.Pow(1 + Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) / 1200,
                    Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) * Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) / 1200 / (((Math.Pow(1 + Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) / 1200,
                    Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) - 1))))))).ToString();
                        }
                        catch (FormatException ex)
                        {
                            string str = "Morate popuniti sva polja!";
                            FormatException format = new FormatException(str, ex);
                            format.Source = this.Text;
                            ExceptionMessageBox box = new ExceptionMessageBox(format);
                            box.Show(this);
                        }

                        _dataService.RaiseSubvencijaUpdatedEvent(0.0d);


                    }

                    if (subvencija.DialogResult == DialogResult.OK)
                    {
                        if (this.ckbSubvencijaOpciono.Checked == false)
                        {
                            iznosSubvencije = 0.1;
                            try{
                            this.kR_ANUITETMaskedTextBox.Text = ((Double.Parse(this.kR_IZNOSTextBox.Text) * ((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) * (Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200 / (((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) - 1))))))).ToString();
                            }
                            catch (FormatException ex)
                            {
                                string str = "Morate popuniti sva polja!";
                                FormatException format = new FormatException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }
                        }
                        if (this.ckbSubvencijaOpciono.Checked == true)
                        {
                            subvencijaOpciono = Convert.ToDouble(this.mtbIznosSubvencije.Text) / 10;
                            iznosSubvencije = subvencijaOpciono;

                            try{
                            this.kR_ANUITETMaskedTextBox.Text = ((Double.Parse(this.kR_IZNOSTextBox.Text) * ((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) * (Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200 / (((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) - 1))))))).ToString();
                            }
                            catch (FormatException ex)
                            {
                                string str = "Morate popuniti sva polja!";
                                FormatException format = new FormatException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }
                        }



                        if (this.ckbSubvencijaOpciono.Checked == false)
                        {
                            _dataService.RaiseSubvencijaUpdatedEvent(0.1d);

                        }
                        if (this.ckbSubvencijaOpciono.Checked == true)
                        {
                            subvencijaOpciono = Convert.ToDouble(this.mtbIznosSubvencije.Text) / 10;
                            _dataService.RaiseSubvencijaUpdatedEvent(subvencijaOpciono);
                        }


                    }





                    try
                    {
                        this.zK_UKUPAN_IZNOSTextBox.Text = (Double.Parse(this.kR_ANUITETMaskedTextBox.Text) *      Double.Parse(this.kR_ROK_OTPLATETextBox.Text)).ToString();
                        this.kR_PROTIVVREDNOSTTextBox.Text = (Decimal.Parse(this.kR_IZNOSTextBox.Text) * Decimal.Parse(this.kUR_SREDNJIComboBox.Text)).ToString();
                        this.zK_KREDITNA_SPOSOBNOSTTextBox.Text = ((Double.Parse(this.zK_PRIMANJA_REDOVNATextBox.Text) + Double.Parse(this.zK_PRIMANJA_VANREDNATextBox.Text)) / 2).ToString();
                    }
                    catch (FormatException ex)
                    {
                        string str = "Morate popuniti sva polja!";
                        FormatException format = new FormatException(str, ex);
                        format.Source = this.Text;
                        ExceptionMessageBox box = new ExceptionMessageBox(format);
                        box.Show(this);
                    }
                  

                    this.Validate();
                    try
                    {
                        this.zAHTEV_ZA_KREDITBindingSource.EndEdit();
                    }
                    catch (NoNullAllowedException ex)
                    {
                        string str = "Morate popuniti sva polja!";
                        NoNullAllowedException format = new NoNullAllowedException(str, ex);
                        format.Source = this.Text;
                        ExceptionMessageBox box = new ExceptionMessageBox(format);
                        box.Show(this);
                    }
                    catch (ConstraintException ex)
                    {
                        string str = "Zahtev sa ovim brojem vec postoji u tabeli!Ne mozete uneti dva puta isti zahtev!!";
                        ConstraintException format = new ConstraintException(str, ex);
                        format.Source = this.Text;
                        ExceptionMessageBox box = new ExceptionMessageBox(format);
                        box.Show(this);
                    }
                    if (!System.Convert.ToBoolean(this.zAHTEV_ZA_KREDITTableAdapter.Update(this.bankJobDataSet.ZAHTEV_ZA_KREDIT) > 0))
                    {
                        MessageBox.Show("Neuspesan UPDATE.");
                    }

                  
                    try
                    {
                        _instance.TryUnload();
                        MessageBox.Show("Zahtev je prosledjen sluzbi za obradu zahteva");

                    } // try
                    catch (Exception ex)
                    {
                        MessageBox.Show(String.Format("Greska prilikom unloadovanja workflowa" +
                        " instance: '{0}'", ex.Message));
                    }
                }// catch
               
            }

          
    This is only the code related to the workflow .Yes,I am sure that workflows are already persisted when i click to the save button .I have problem only when I reopen the same form and click to the save button .
    Some words in the code are in Serbian language . I hope that you would not be confused by that . All the code related to the workflow is in English.     
         
    Thank you for your efforts !
  • Saturday, October 31, 2009 3:44 PMGeert van Horrik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    At which line does it get terminated? At the Guid workflowInstanceId = this.SubmitToWorkflow();, or later?

    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
  • Saturday, October 31, 2009 4:17 PMmilan82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Later. In  this line .
    --- subvencija.ShowDialog();

    First this Dialog pops up,and then I get MessageBox for wr_WorkflowTerminated Event.
  • Saturday, October 31, 2009 4:19 PMGeert van Horrik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    So the dialog is shown. Does the message show up while your dialog is still visible, or after you close the dialog? If you comment the ShowDialog() code, does it still terminate?
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
  • Saturday, October 31, 2009 4:33 PMmilan82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes .If I comment the ShowDialog() code,  it still terminate my workflow .
  • Saturday, October 31, 2009 4:35 PMGeert van Horrik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I think these lines are causing the problem:

    Guid workflowInstanceId = this.SubmitToWorkflow();
    
    
    this.zK_INSTANCA_WFTextBox.Text = workflowInstanceId.ToString();
    service.RaiseStatusUObradi(workflowInstanceId, brojZahteva, status, subvencijaZahteva);
    

    Is it the just created workflow (in SubmitToWorkflow) that is terminated, or is it another one?
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
  • Saturday, October 31, 2009 4:39 PMmilan82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Just created workflow (in SubmitToWorkflow)  is terminated ,and this is happening only when i reopen the form . When i first open the form everything works fine .
  • Saturday, October 31, 2009 4:47 PMGeert van Horrik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You are just creating one Runtime, right?
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
  • Saturday, October 31, 2009 5:06 PMmilan82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    In this form i have just one runtime . To test my application you must to enter all data related to this form manualy and for doing that it takes time . Not to mention that all my application is in Serbian language :) I don't want to bother you with that .
    Thanks.
  • Sunday, November 01, 2009 3:45 AMmilan82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Problem is solved .

    It was WorkflowRuntime problem.





  • Sunday, November 01, 2009 10:02 AMGeert van Horrik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for letting us know that your problem is solved. Can you please explain what you did to solve your problem? Then, mark that reply as answer so this thread is closed.
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
  • Sunday, November 01, 2009 10:51 AMmilan82 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The form  for which i posted the code was not my  application main form code . So, every time when i was loading this form,i was calling this method:


                this.StartWorkflowRuntime();

    ,and this method have this line :


                _workflowRuntime = new WorkflowRuntime();

    I think that you understand now what was the problem :) I was looking the solution in the wrong place .

    I will post only the code related to WWF  .


    First, the WorkflowFactory class :

    public static class WorkflowFactory
        {

             // Singleton instance of the workflow runtime
                private static WorkflowRuntime _workflowRuntime = null;
                //BankJobDataConnector service;

                // Lock (sync) object
                private static object _syncRoot = new object();


                private static AutoResetEvent _waitHandle = new AutoResetEvent(false);

                // Factory method
                public static WorkflowRuntime GetWorkflowRuntime()
                {
                    // Lock execution thread in case of multi-threaded
                    // (concurrent) access.
                    lock (_syncRoot)
                    {
                        // Check for startup condition
                        if (null == _workflowRuntime)
                        {
                            // Provide for shutdown
                            AppDomain.CurrentDomain.ProcessExit += new EventHandler(StopWorkflowRuntime);
                            AppDomain.CurrentDomain.DomainUnload += new EventHandler(StopWorkflowRuntime);

                            // Not started, so create instance
                           
                            //_workflowRuntime.WorkflowIdled += new EventHandler<WorkflowEventArgs>(wr_WorkflowIdled);

                            // Add the external data service
                            _workflowRuntime = new WorkflowRuntime();

                        
                            SqlWorkflowPersistenceService stateService = new SqlWorkflowPersistenceService(
                                PersistanceServiceSettings.ConnectionString);
                            _workflowRuntime.AddService(stateService);

                            SqlTrackingService trackingService = new SqlTrackingService(PersistanceServiceSettings.ConnectionString);
                            _workflowRuntime.AddService(trackingService);

                            SharedConnectionWorkflowCommitWorkBatchService sharedService = new  SharedConnectionWorkflowCommitWorkBatchService(PersistanceServiceSettings.ConnectionString);
                            _workflowRuntime.AddService(sharedService);
                            // Start the Workflow services
                            _workflowRuntime.StartRuntime();
                        } // if

                        // Return singleton instance
                        return _workflowRuntime;
                    } // lock
                }

                // Shutdown method
                static void StopWorkflowRuntime(object sender, EventArgs e)
                {
                    if (_workflowRuntime != null)
                    {
                        if (_workflowRuntime.IsStarted)
                        {
                            try
                            {
                                // Stop the runtime
                                _workflowRuntime.StopRuntime();
                            }
                            catch (ObjectDisposedException)
                            {
                                // Already disposed of, so ignore...
                            } // catch
                        } // if
                    } // if
                }
               
            }

    Second,the form class(only the part related to to WWF) :

            WorkflowBankJobDataService _dataService;
            WorkflowRuntime _workflowRuntime;
            WorkflowInstance _instance;
           
            BankJobDataConnector service;
            private Dictionary<string, StateMachineWorkflowInstance> stateMachineInstances;


     


     private Guid SubmitToWorkflow()
            {

                Int32 brojZahteva = Convert.ToInt32(this.zK_BROJTextBox.Text);
                string statusZahteva = this.zK_BROJTextBox.Text;
              
              
               
                // Fill the Parameters collection for this instance of the workflow
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("IDZahteva", brojZahteva);
                parameters.Add("StatusZahteva", statusZahteva);
          

             

                // Start the workflow instance
                _instance = this._workflowRuntime.CreateWorkflow(typeof(BankJobWorkflow.BankJobWorkflow), parameters);

                StateMachineWorkflowInstance stateMachineInstance = new StateMachineWorkflowInstance(_workflowRuntime, _instance.InstanceId);

                _dataService = BankJobDataService.WorkflowBankJobDataService.CreateDataService(_instance.InstanceId, _workflowRuntime);
                _dataService.SubvencijaUpdated += new EventHandler<SubvencijaEventArgs>(dataConnectorService_SubvencijaUpdated);

                _instance.Start();
             

                // Add the StateMachineInstance object for our Workflow to our dictionary
                this.stateMachineInstances.Add(_instance.InstanceId.ToString(), stateMachineInstance);

                // Return the WorkflowInstanceId
                return _instance.InstanceId;
            }

    void dataConnectorService_SubvencijaUpdated(object sender, BankJobDataService.SubvencijaEventArgs e)
            {
                IAsyncResult result = this.BeginInvoke(
                    new EventHandler(
                           delegate
                           {
                               this.zK_SUBVENCIJAMaskedTextBox.Text = (e.Subvencija).ToString();
                              
                           } // delegate
                        ), null, null
                ); // BeginInvoke
                this.EndInvoke(result);
            }

    void wr_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)
            {

               
                MessageBox.Show(string.Format("Workflow je terminiran, sa workflow instancom={0}", e.Exception.Message), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
               
              
           }

            void wr_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
            {
               
                MessageBox.Show(string.Format("Workflow je kompletiran, sa workflow instancom={0}", e.WorkflowInstance.InstanceId), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
               
            }


    private void frmZahtevZaKredit_Load(object sender, EventArgs e)
            {
              

               
                _workflowRuntime = WorkflowFactory.GetWorkflowRuntime();

                _workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(wr_WorkflowCompleted);
                _workflowRuntime.WorkflowTerminated += new EventHandler<WorkflowTerminatedEventArgs>(wr_WorkflowTerminated);
              
                // Create a new dictionary to store the StateMachineInstance objects
                this.stateMachineInstances = new Dictionary<string, StateMachineWorkflowInstance>();

              
              
              
            }

    private void btnSacuvaj_Click(object sender, EventArgs e)
            {
                int brojZahteva = Convert.ToInt32(this.zK_BROJTextBox.Text);
                string status = this.zK_STATUSComboBox.Text;
                string subvencijaZahteva = this.zK_SUBVENCIJAMaskedTextBox.Text;
                double iznosSubvencije;
                double subvencijaOpciono;
             
               

                if (status != "U OBRADI")
                {
                    MessageBox.Show("Odabrani zahtev je obradjen!");
                }
                if (status == "U OBRADI")
                {
                    if (this.zK_BROJTextBox.Text != "")
                    {



                        Guid workflowInstanceId =
                        this.SubmitToWorkflow();



                        this.zK_INSTANCA_WFTextBox.Text = workflowInstanceId.ToString();
                        service = (BankJobDataConnector)_workflowRuntime.GetService(typeof(BankJobDataConnector));
                        service.RaiseStatusUObradi(workflowInstanceId, brojZahteva, status, subvencijaZahteva);
                    }

                    frmSubvencija subvencija = new frmSubvencija();

                    subvencija.ShowDialog();
                    if (subvencija.DialogResult == DialogResult.Cancel)
                    {
                       
                        try{
                        this.kR_ANUITETMaskedTextBox.Text = ((Double.Parse(this.kR_IZNOSTextBox.Text) * ((Math.Pow(1 + Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) / 1200,
                    Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) * Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) / 1200 / (((Math.Pow(1 + Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) / 1200,
                    Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) - 1))))))).ToString();
                        }
                        catch (FormatException ex)
                        {
                            string str = "Morate popuniti sva polja!";
                            FormatException format = new FormatException(str, ex);
                            format.Source = this.Text;
                            ExceptionMessageBox box = new ExceptionMessageBox(format);
                            box.Show(this);
                        }

                        //this.zK_SUBVENCIJAMaskedTextBox.Text = "0.0";
                        try
                        {
                            _dataService.RaiseSubvencijaUpdatedEvent(0.0d);
                        }
                        catch (InvalidOperationException ex)
                        {
                            string str = "IZABRANI WORKFLOW VISE NIJE PRISUTAN U BAZI";
                            InvalidOperationException format = new InvalidOperationException(str, ex);
                            format.Source = this.Text;
                            ExceptionMessageBox box = new ExceptionMessageBox(format);
                            box.Show(this);
                        }


                    }

                    if (subvencija.DialogResult == DialogResult.OK)
                    {
      
                        if (this.ckbSubvencijaOpciono.Checked == false)
                        {
                            iznosSubvencije = 0.1;
                            try{
                            this.kR_ANUITETMaskedTextBox.Text = ((Double.Parse(this.kR_IZNOSTextBox.Text) * ((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) * (Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200 / (((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) - 1))))))).ToString();
                            }
                            catch (FormatException ex)
                            {
                                string str = "Morate popuniti sva polja!";
                                FormatException format = new FormatException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }
                        }
                        if (this.ckbSubvencijaOpciono.Checked == true)
                        {
                            subvencijaOpciono = Convert.ToDouble(this.mtbIznosSubvencije.Text) / 10;
                            iznosSubvencije = subvencijaOpciono;

                            try{
                            this.kR_ANUITETMaskedTextBox.Text = ((Double.Parse(this.kR_IZNOSTextBox.Text) * ((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) * (Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200 / (((Math.Pow(1 + ((Double.Parse(this.kR_IZNOS_KAMATEMaskedTextBox.Text) - iznosSubvencije) / 1200),
                   Double.Parse(this.kR_ROK_OTPLATETextBox.Text)) - 1))))))).ToString();
                            }
                            catch (FormatException ex)
                            {
                                string str = "Morate popuniti sva polja!";
                                FormatException format = new FormatException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }
                        }



                        if (this.ckbSubvencijaOpciono.Checked == false)
                        {
                            //this.zK_SUBVENCIJAMaskedTextBox.Text = "0.1";
                            try
                            {
                                _dataService.RaiseSubvencijaUpdatedEvent(0.1d);
                            }
                            catch (InvalidOperationException ex)
                            {
                                string str = "IZABRANI WORKFLOW VISE NIJE PRISUTAN U BAZI";
                                InvalidOperationException format = new InvalidOperationException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }

                        }
                        if (this.ckbSubvencijaOpciono.Checked == true)
                        {
                            subvencijaOpciono = Convert.ToDouble(this.mtbIznosSubvencije.Text) / 10;
                            try
                            {
                                _dataService.RaiseSubvencijaUpdatedEvent(subvencijaOpciono);
                            }
                            catch (InvalidOperationException ex)
                            {
                                string str = "IZABRANI WORKFLOW VISE NIJE PRISUTAN U BAZI";
                                InvalidOperationException format = new InvalidOperationException(str, ex);
                                format.Source = this.Text;
                                ExceptionMessageBox box = new ExceptionMessageBox(format);
                                box.Show(this);
                            }
                        }


                    }





                    try
                    {
                        this.zK_UKUPAN_IZNOSTextBox.Text = (Double.Parse(this.kR_ANUITETMaskedTextBox.Text) * Double.Parse(this.kR_ROK_OTPLATETextBox.Text)).ToString();
                        this.kR_PROTIVVREDNOSTTextBox.Text = (Decimal.Parse(this.kR_IZNOSTextBox.Text) * Decimal.Parse(this.kUR_SREDNJIComboBox.Text)).ToString();
                        this.zK_KREDITNA_SPOSOBNOSTTextBox.Text = ((Double.Parse(this.zK_PRIMANJA_REDOVNATextBox.Text) + Double.Parse(this.zK_PRIMANJA_VANREDNATextBox.Text)) / 2).ToString();
                    }
                    catch (FormatException ex)
                    {
                        string str = "Morate popuniti sva polja!";
                        FormatException format = new FormatException(str, ex);
                        format.Source = this.Text;
                        ExceptionMessageBox box = new ExceptionMessageBox(format);
                        box.Show(this);
                    }
                    // _workflowRuntime.WorkflowIdled += new EventHandler<WorkflowEventArgs>(wr_WorkflowIdled);

                    this.Validate();
                    try
                    {
                        this.zAHTEV_ZA_KREDITBindingSource.EndEdit();
                    }
                    catch (NoNullAllowedException ex)
                    {
                        string str = "Morate popuniti sva polja!";
                        NoNullAllowedException format = new NoNullAllowedException(str, ex);
                        format.Source = this.Text;
                        ExceptionMessageBox box = new ExceptionMessageBox(format);
                        box.Show(this);
                    }
                    catch (ConstraintException ex)
                    {
                        string str = "Zahtev sa ovim brojem vec postoji u tabeli!Ne mozete uneti dva puta isti zahtev!!";
                        ConstraintException format = new ConstraintException(str, ex);
                        format.Source = this.Text;
                        ExceptionMessageBox box = new ExceptionMessageBox(format);
                        box.Show(this);
                    }
                    if (!System.Convert.ToBoolean(this.zAHTEV_ZA_KREDITTableAdapter.Update(this.bankJobDataSet.ZAHTEV_ZA_KREDIT) > 0))
                    {
                        MessageBox.Show("Neuspesan UPDATE.");
                    }

                    //  UnloadInstance(workflowInstanceId);
                    try
                    {
                        _instance.TryUnload();
                      
                        MessageBox.Show("Zahtev je prosledjen sluzbi za obradu zahteva");

                    } // try
                    catch (Exception ex)
                    {
                        MessageBox.Show(String.Format("Greska prilikom unloadovanja workflowa" +
                        " instance: '{0}'", ex.Message));
                    }
                }// catch
               
            }


    The second workflow had similar problems so i won't posting the code.

    Regards .





    • Marked As Answer bymilan82 Sunday, November 01, 2009 10:51 AM
    •