none
How to get result when the workflow is completed in Asp.net MVC application RRS feed

  • Question

  • Hello all,

    I want to get the result when the WF is successfully completed.

    My workflow works well, it sends e-mail the employee and change record in data base but I can't see message in the view (page) saying "approved Successfully"?

    This is a method in the controller to approve an Employee:

         [HttpPost]
            public ActionResult Approuve(int id)
            {
                using (DBModel db = new DBModel())
                {
                    SqlConnection conString = new SqlConnection(db.Database.Connection.ConnectionString);
                    Employee emp = db.Employees.Where(x => x.EmployeeID == id).FirstOrDefault<Employee>();
                    int resultWF;
                    resultWF=OrdreWF(true,"mgh@gmail.com",emp.EmployeeID, "Outlook","","","WF ASP NET: Employee approuved","this email to test wf","","","", conString);
                    
                    if(resultWF==1)
                    return Json(new { success = true, message = "approved Successfully" }, JsonRequestBehavior.AllowGet);
                  
                }
            }
    
     public int OrdreWF(bool ordre, string email, int id, string smtptype, string from, string passwrd, string subject, string body, string idlancer, string idtache, string idress, SqlConnection stringConnection)
            {
                int result = 0;
                try
                {
                   
                    AutoResetEvent syncEvent = new AutoResetEvent(false);
                    AutoResetEvent idleEvent = new AutoResetEvent(false);
    
                    var inputs = new Dictionary<string, object>() { { "Approuver", ordre }, { "EmailTo", email }, { "id", id }, { "smtp_type", smtptype }, { "body_msg", body }, { "senderAddress", from }, { "Password", passwrd }, { "Subject_msg", subject }, { "IDlanceur", idlancer }, { "IDtache", idtache }, { "IDresTache", idress }, { "stringConnection", stringConnection } };
                    //approved
                    WorkflowApplication wfApp =
                        new WorkflowApplication(new TaskWF1(), inputs);//c'est le workflow 
    
                    wfApp.Completed = delegate (WorkflowApplicationCompletedEventArgs evt)
                    {
    
                        if (ordre)
                            result = 1;
                        else
                            result = 2;
             
                      
                        syncEvent.Set();
                    };
    
                    wfApp.Aborted = delegate (WorkflowApplicationAbortedEventArgs evt)
                    {
                        //Cursor.Current = Cursors.Default;
                        Console.WriteLine(evt.Reason);
                        
                        syncEvent.Set();
                    };
    
                    wfApp.OnUnhandledException = delegate (WorkflowApplicationUnhandledExceptionEventArgs evt)
                    {
                        //Cursor.Current = Cursors.Default;
                        Console.WriteLine(evt.UnhandledException.ToString());
                        return UnhandledExceptionAction.Terminate;
                    };
    
                    wfApp.Idle = delegate (WorkflowApplicationIdleEventArgs evt)
                    {
                        //Cursor.Current = Cursors.Default;
                        idleEvent.Set();
                    };
    
                    wfApp.Run();
                }
                catch (Exception ex)
                {
                    result = -1;
                    
                }
                return result;
            }
    
        }

    ##### WF xaml#################

    <Activity mc:Ignorable="sap sap2010 sads" x:Class="Workflow1Niveau.TaskWF1"
     xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
     xmlns:local="clr-namespace:WorkflowApplication1"
     xmlns:local1="clr-namespace:;assembly=ActivityLibrary"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:mca="clr-namespace:Microsoft.CSharp.Activities;assembly=System.Activities"
     xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
     xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
     xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
     xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
     xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
     xmlns:sds="clr-namespace:System.Data.SqlClient;assembly=System.Data"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <x:Members>
        <x:Property Name="Approuver" Type="InArgument(x:Boolean)" />
        <x:Property Name="EmailTo" Type="InArgument(x:String)" />
        <x:Property Name="id" Type="InArgument(x:Int32)" />
        <x:Property Name="smtp_type" Type="InArgument(x:String)" />
        <x:Property Name="body_msg" Type="InArgument(x:String)" />
        <x:Property Name="senderAddress" Type="InArgument(x:String)" />
        <x:Property Name="Password" Type="InArgument(x:String)" />
        <x:Property Name="Subject_msg" Type="InArgument(x:String)" />
        <x:Property Name="IDlanceur" Type="InArgument(x:String)" />
        <x:Property Name="IDtache" Type="InArgument(x:String)" />
        <x:Property Name="IDresTache" Type="InArgument(x:String)" />
        <x:Property Name="stringConnection" Type="InArgument(sds:SqlConnection)" />
      </x:Members>
      <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor>
      <sap2010:WorkflowViewState.IdRef>Workflow1Niveau.TaskWF1_1</sap2010:WorkflowViewState.IdRef>
      <TextExpression.NamespacesForImplementation>
        <sco:Collection x:TypeArguments="x:String">
          <x:String>System</x:String>
          <x:String>System.Collections.Generic</x:String>
          <x:String>System.Data</x:String>
          <x:String>System.Linq</x:String>
          <x:String>System.Text</x:String>
          <x:String>System.Runtime.InteropServices</x:String>
          <x:String>ActivityLibrary</x:String>
          <x:String>System.Activities</x:String>
          <x:String>System.Runtime</x:String>
          <x:String>System.Collections.Concurrent</x:String>
          <x:String>Workflow1Niveau</x:String>
          <x:String>WorkflowApplication1</x:String>
          <x:String>System.Data.SqlClient</x:String>
        </sco:Collection>
      </TextExpression.NamespacesForImplementation>
      <TextExpression.ReferencesForImplementation>
        <sco:Collection x:TypeArguments="AssemblyReference">
          <AssemblyReference>Microsoft.CSharp</AssemblyReference>
          <AssemblyReference>PresentationCore</AssemblyReference>
          <AssemblyReference>PresentationFramework</AssemblyReference>
          <AssemblyReference>System</AssemblyReference>
          <AssemblyReference>System.Activities</AssemblyReference>
          <AssemblyReference>System.Activities.Presentation</AssemblyReference>
          <AssemblyReference>System.Core</AssemblyReference>
          <AssemblyReference>System.Data</AssemblyReference>
          <AssemblyReference>System.Runtime.Serialization</AssemblyReference>
          <AssemblyReference>System.ServiceModel</AssemblyReference>
          <AssemblyReference>System.ServiceModel.Activities</AssemblyReference>
          <AssemblyReference>System.Xaml</AssemblyReference>
          <AssemblyReference>System.Xml</AssemblyReference>
          <AssemblyReference>System.Xml.Linq</AssemblyReference>
          <AssemblyReference>WindowsBase</AssemblyReference>
          <AssemblyReference>ActivityLibrary</AssemblyReference>
          <AssemblyReference>mscorlib</AssemblyReference>
          <AssemblyReference>Workflow1Niveau</AssemblyReference>
        </sco:Collection>
      </TextExpression.ReferencesForImplementation>
      <Sequence sap2010:WorkflowViewState.IdRef="Sequence_2">
        <Sequence.Variables>
          <Variable x:TypeArguments="local:ApproveClass" Name="className">
            <Variable.Default>
              <mca:CSharpValue x:TypeArguments="local:ApproveClass">new WorkflowApplication1.ApproveClass()</mca:CSharpValue>
            </Variable.Default>
          </Variable>
        </Sequence.Variables>
        <Switch x:TypeArguments="x:Boolean" sap2010:WorkflowViewState.IdRef="Switch`1_1">
          <Switch.Expression>
            <InArgument x:TypeArguments="x:Boolean">
              <mca:CSharpValue x:TypeArguments="x:Boolean">Approuver</mca:CSharpValue>
            </InArgument>
          </Switch.Expression>
          <Sequence x:Key="True" sap2010:WorkflowViewState.IdRef="Sequence_1">
            <Sequence.Variables>
              <Variable x:TypeArguments="x:Int32" Name="id2" />
              <Variable x:TypeArguments="x:String" Name="variable1" />
            </Sequence.Variables>
            <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_1" Text="Employé approuvé" />
            <local1:SendEmailActivity result="{x:Null}" DisplayName="Send mail to the project manager" sap2010:WorkflowViewState.IdRef="SendEmailActivity_1" host="smtp.gmail.com">
              <local1:SendEmailActivity.SMTPType>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">smtp_type</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.SMTPType>
              <local1:SendEmailActivity.body>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">body_msg</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.body>
              <local1:SendEmailActivity.from>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">senderAddress</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.from>
              <local1:SendEmailActivity.password>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">Password</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.password>
              <local1:SendEmailActivity.subject>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">Subject_msg</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.subject>
              <local1:SendEmailActivity.to>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">EmailTo</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.to>
              <local1:SendEmailActivity.userName>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">senderAddress</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.userName>
            </local1:SendEmailActivity>
            <InvokeMethod DisplayName="Update employee aproved in DB" sap2010:WorkflowViewState.IdRef="InvokeMethod_1" MethodName="UpdateStateApproved">
              <InvokeMethod.TargetObject>
                <InArgument x:TypeArguments="local:ApproveClass">
                  <mca:CSharpValue x:TypeArguments="local:ApproveClass">className</mca:CSharpValue>
                </InArgument>
              </InvokeMethod.TargetObject>
              <InArgument x:TypeArguments="x:Int32">
                <mca:CSharpValue x:TypeArguments="x:Int32">id</mca:CSharpValue>
              </InArgument>
              <InArgument x:TypeArguments="sds:SqlConnection">
                <mca:CSharpValue x:TypeArguments="sds:SqlConnection">stringConnection</mca:CSharpValue>
              </InArgument>
            </InvokeMethod>
          </Sequence>
          <Sequence x:Key="False" sap2010:WorkflowViewState.IdRef="Sequence_3">
            <Sequence.Variables>
              <Variable x:TypeArguments="x:Int32" Name="id2" />
              <Variable x:TypeArguments="x:String" Name="variable1" />
            </Sequence.Variables>
            <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_2" Text="Employé Refused" />
            <local1:SendEmailActivity result="{x:Null}" DisplayName="Envoyer email au chef de projet" sap2010:WorkflowViewState.IdRef="SendEmailActivity_2" host="smtp.gmail.com">
              <local1:SendEmailActivity.SMTPType>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">smtp_type</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.SMTPType>
              <local1:SendEmailActivity.body>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">body_msg</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.body>
              <local1:SendEmailActivity.from>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">senderAddress</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.from>
              <local1:SendEmailActivity.password>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">Password</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.password>
              <local1:SendEmailActivity.subject>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">Subject_msg</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.subject>
              <local1:SendEmailActivity.to>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">EmailTo</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.to>
              <local1:SendEmailActivity.userName>
                <InArgument x:TypeArguments="x:String">
                  <mca:CSharpValue x:TypeArguments="x:String">senderAddress</mca:CSharpValue>
                </InArgument>
              </local1:SendEmailActivity.userName>
            </local1:SendEmailActivity>
            <InvokeMethod DisplayName="Update employee refused in DB" sap2010:WorkflowViewState.IdRef="InvokeMethod_3" MethodName="UpdateStateRejected">
              <InvokeMethod.TargetObject>
                <InArgument x:TypeArguments="local:ApproveClass">
                  <mca:CSharpValue x:TypeArguments="local:ApproveClass">className</mca:CSharpValue>
                </InArgument>
              </InvokeMethod.TargetObject>
              <InArgument x:TypeArguments="x:Int32">
                <mca:CSharpValue x:TypeArguments="x:Int32">id</mca:CSharpValue>
              </InArgument>
              <InArgument x:TypeArguments="sds:SqlConnection">
                <mca:CSharpValue x:TypeArguments="sds:SqlConnection">stringConnection</mca:CSharpValue>
              </InArgument>
            </InvokeMethod>
          </Sequence>
        </Switch>
        
    </Activity>

    Wednesday, September 4, 2019 10:47 AM