I've just started working with Workflow (WF4) and have been playing with an idea of using it in MVC3.0 controller actions to see if it improves the maintainability of complex actions; also potentially giving the ability to make multiple DB calls in parallel to populate the output model. Along with this I've been looking at Async controller actions but I'm not sure I've really got my head around those. I know you can load workflow into a WorkflowApplication and call BeginRun(...) to get that to run asynchronously but I'm thinking that's the wrong approach; from looking online I've come up with the following implementation which does run as expected but I'm wondering if there's anything wrong with the code below or it will cause other issues that I've not thought about.
[HttpPost]
public void ConfigureAsync(ConfigureExportInputModel inputModel)
{
if (inputModel == null)
{
throw new ArgumentNullException("inputModel");
}
var arguments = new Dictionary<string, object> { { "ExportType", inputModel.ExportType } };
var wf = new ErrorHandledExportWorkflow();
AsyncManager.OutstandingOperations.Increment();
ThreadPool.QueueUserWorkItem(o =>
{
AsyncManager.Parameters["result"] = WorkflowInvoker.Invoke(wf, arguments)["MvcOutput"] as IDefineMvcOutput;
AsyncManager.OutstandingOperations.Decrement();
});
}
public ActionResult ConfigureCompleted(IDefineMvcOutput result)
{
return this.ActionResultFactory.Create(this, result);
}
Contract.Requires(inputModel != null)
which is more semantic and has interesting AOP possibilities. For example you can have all these checks fire in development and testing and disabled to boost performance in production. There is also a VS extension that will attempt to do some basic analysis on the code contracts while your coding and point out gaps in logic. – George Mauer Feb 7 '12 at 16:28