开发者

Show a popup message after completion of WorkFlow 4 from the controller MVC

开发者 https://www.devze.com 2023-03-25 04:38 出处:网络
I am a beginner in WorkFlow 4 (WF 4), I am running through a serious issue using it in MVC 3, I couldn\'t find the answer online.

I am a beginner in WorkFlow 4 (WF 4), I am running through a serious issue using it in MVC 3, I couldn't find the answer online.

I need to show a popup message if an exception occurred in the workFlow or anything returned in the outpput arguments, I have a page that the user would edit and at the end he will click Save button.

Clicking the Save button will submit the form to the controller and run a workflow, when the workflow completed, I got an output explaining if updating the data through the workflow succeeded or not, then I need to show this status on completed action, but I am unable to since I run it asynchronously which means the method will return to the user and in parallel the workflow is invoking the event.

Here is my code in the controller:

[HttpPost()]
    public ActionResult SaveVehicles(vehiclesData model) {
   Services.VehiclesDataUpdate vehiclesDataUpdate = new Services.VehiclesDataUpdate(this.SessionData.DealerLotKey, null, null);
            IDictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("VehiclesDataUpdate", vehiclesDataUpdate);
            parameters.Add("UnionVehicles", unionVehicles);
            parameters.Add("SolrVehicles", solrVehicles);

            IDictionary<string, object> outputs = new Dictionary<string, object>();
            AutoResetEvent syncEvent = new AutoResetEvent(false);
            WorkflowApplication wfApp = new WorkflowApplication(new VehiclesUpdate(), parameters);

            wfApp.Completed = delegate(Work开发者_运维百科flowApplicationCompletedEventArgs e) {
                outputs = e.Outputs;
                 syncEvent.Set();

                if (!errorExceptions.IsNullOrEmpty()) {
                    //TODO: Render a parital view to display an error message or the result of the workflow in the ouptput
                    //TODO: Logging.
                }
            };

            wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e) {
                syncEvent.Set();
            };

            wfApp.Run();

 return View(model);
    }

How can I send something back to the user when workflow completion?

Thanks in advance.


If you want to run a workflow from an MVC action there are several ways you can do so. First fo all you can use the WorkflowApplication as you did. I have adapted the code to use an AyncController that is a better fit with a WorkflowApplication.

public class HomeController : AsyncController
{
    [HttpPost()]
    public void SaveVehiclesAsync(vehiclesData model)
    {
        Services.VehiclesDataUpdate vehiclesDataUpdate = new Services.VehiclesDataUpdate(this.SessionData.DealerLotKey, null, null);
        IDictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("VehiclesDataUpdate", vehiclesDataUpdate);
        parameters.Add("UnionVehicles", unionVehicles);
        parameters.Add("SolrVehicles", solrVehicles);

        WorkflowApplication wfApp = new WorkflowApplication(new VehiclesUpdate(), parameters);

        wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e) {
            AsyncManager.Parameters["outputs"] = e.Outputs;
            AsyncManager.OutstandingOperations.Decrement();
        };

        wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e) {
           AsyncManager.OutstandingOperations.Decrement();
        };

        AsyncManager.OutstandingOperations.Increment();
        wfApp.Run();
    }

    public ActionResult IndexCompleted(IDictionary<string, object> outputs)
    {
        var model = outputs["model"];
        return View(model);
    }
}

However that might not always be the best solution. In general an ASP.NET server is going to be busy enough to keep all cores happy so asynchronous processing is only going to hurt scale ability. Now if your workflow is doing mainly async IO this is fine but if it is doing synchronous IO or processing it might be better to use a WorkflowInvoker instead.

The code would be something like this.

public class HomeController : Controller
{
    [HttpPost()]
    public ActionResult SaveVehicles(vehiclesData model)
    {
        Services.VehiclesDataUpdate vehiclesDataUpdate = new Services.VehiclesDataUpdate(this.SessionData.DealerLotKey, null, null);
        IDictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("VehiclesDataUpdate", vehiclesDataUpdate);
        parameters.Add("UnionVehicles", unionVehicles);
        parameters.Add("SolrVehicles", solrVehicles);

        IDictionary<string, object> outputs = WorkflowInvoker.Invoke(new VehiclesUpdate(), parameters);

        var model = outputs["model"];
        return View(model);
    }
}

Note: Did the code with just Notepad++ so be aware of small syntax errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消