Asp.net mvc 从控制器MVC完成工作流4后显示弹出消息
我是工作流4(WF4)的初学者,在MVC3中使用它时遇到了一个严重的问题,我在网上找不到答案 我需要显示一个弹出消息,如果工作流中发生异常或Output参数中返回的任何内容,我有一个用户将编辑的页面,最后他将单击保存按钮 单击保存按钮将表单提交给控制器并运行工作流,当工作流完成时,我得到一个输出,解释通过工作流更新数据是否成功,然后我需要在完成操作时显示此状态,但是我不能这样做,因为我异步运行它,这意味着该方法将返回给用户,同时工作流正在调用该事件 以下是我在控制器中的代码:Asp.net mvc 从控制器MVC完成工作流4后显示弹出消息,asp.net-mvc,workflow-foundation-4,workflowservice,Asp.net Mvc,Workflow Foundation 4,Workflowservice,我是工作流4(WF4)的初学者,在MVC3中使用它时遇到了一个严重的问题,我在网上找不到答案 我需要显示一个弹出消息,如果工作流中发生异常或Output参数中返回的任何内容,我有一个用户将编辑的页面,最后他将单击保存按钮 单击保存按钮将表单提交给控制器并运行工作流,当工作流完成时,我得到一个输出,解释通过工作流更新数据是否成功,然后我需要在完成操作时显示此状态,但是我不能这样做,因为我异步运行它,这意味着该方法将返回给用户,同时工作流正在调用该事件 以下是我在控制器中的代码: [HttpPost
[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(WorkflowApplicationCompletedEventArgs 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);
}
[HttpPost()]
公共行动结果保存车辆(车辆数据模型){
Services.VehiclesDataUpdate VehiclesDataUpdate=新服务.VehiclesDataUpdate(this.SessionData.DealerLotKey,null,null);
IDictionary参数=新字典();
参数。添加(“车辆数据更新”,车辆数据更新);
参数。添加(“UnionVehicles”,UnionVehicles);
参数。添加(“SolrVehicles”,SolrVehicles);
IDictionary输出=新字典();
AutoResetEvent syncEvent=新的AutoResetEvent(假);
WorkflowApplication wfApp=新的WorkflowApplication(新车辆更新(),参数);
wfApp.Completed=委托(WorkflowApplicationCompletedEventArgs e){
输出=e.输出;
syncEvent.Set();
如果(!errorExceptions.IsNullOrEmpty()){
//TODO:渲染一个局部视图以在输出中显示错误消息或工作流结果
//TODO:日志记录。
}
};
wfApp.Aborted=委托(WorkflowApplicationAbortedEventArgs e){
syncEvent.Set();
};
wfApp.Run();
返回视图(模型);
}
工作流完成后,如何将内容发送回用户
提前感谢。如果您想从MVC操作运行工作流,有几种方法可以做到这一点。首先,您可以像以前一样使用Workflow应用程序。我已经对代码进行了调整,以使用更适合Workflow应用程序的AyncController
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);
}
}
公共类HomeController:AsyncController
{
[HttpPost()]
public void SaveVehiclesAsync(车辆数据模型)
{
Services.VehiclesDataUpdate VehiclesDataUpdate=新服务.VehiclesDataUpdate(this.SessionData.DealerLotKey,null,null);
IDictionary参数=新字典();
参数。添加(“车辆数据更新”,车辆数据更新);
参数。添加(“UnionVehicles”,UnionVehicles);
参数。添加(“SolrVehicles”,SolrVehicles);
WorkflowApplication wfApp=新的WorkflowApplication(新车辆更新(),参数);
wfApp.Completed=委托(WorkflowApplicationCompletedEventArgs e){
AsyncManager.Parameters[“outputs”]=e.outputs;
AsyncManager.OutstandingOperations.Decrement();
};
wfApp.Aborted=委托(WorkflowApplicationAbortedEventArgs e){
AsyncManager.OutstandingOperations.Decrement();
};
AsyncManager.OutstandingOperations.Increment();
wfApp.Run();
}
公共行动结果索引已完成(索引输出)
{
var模型=输出[“模型”];
返回视图(模型);
}
}
然而,这可能并不总是最好的解决方案。通常,ASP.NET服务器将非常繁忙,足以让所有内核都满意,因此异步处理只会损害扩展能力。现在,如果您的工作流主要执行异步IO,这很好,但如果它执行同步IO或处理,则最好使用WorkflowInvoker
代码应该是这样的
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);
}
}
公共类HomeController:控制器
{
[HttpPost()]
公共行动结果保存车辆(车辆数据模型)
{
Services.VehiclesDataUpdate VehiclesDataUpdate=新服务.VehiclesDataUpdate(this.SessionData.DealerLotKey,null,null);
IDictionary参数=新字典();
参数。添加(“车辆数据更新”,车辆数据更新);
参数。添加(“UnionVehicles”,UnionVehicles);
参数。添加(“SolrVehicles”,SolrVehicles);
IDictionary outputs=WorkflowInvoker.Invoke(新车辆更新(),参数);
var模型=输出[“模型”];
返回视图(模型);
}
}
注意:代码是否仅使用Notepad++,因此请注意小的语法错误。或使用a向ASP.NET worker线程发出工作流应用程序已完成执行的信号。请提供一个示例?这里有很多。浏览[wf4]并阅读。我不会回答,因为我相信在ASP.NET中使用可能会有更好的解决方案。问题是,为了给出一个充分的答案,我对这一点还不够熟悉。@Mautice:首先,非常感谢你的回答,它让我对很多事情都有了了解。我正在采用异步方式。我现在知道它是如何工作的,但SaveVehiclesAsync()是一个无效的方法,不会返回任何东西,在我的情况下,我想在工作流处理时向用户返回一些东西,然后当工作流完成时,我将向用户返回不同的东西。如何实现SaveVehiclesAsync()以返回视图(例如,在工作流完成之前)。查看post,了解在服务器进程仍处于活动状态时向浏览器发送更新的方法。另外,此工作流不应该异步独立运行吗,如果工作流需要一段时间才能完成,并且我试图转到另一个页面,那么在工作流完成之前,它将不会继续,对吗?不,就