While facing a scenario where-in I wanted to invoke a workflow asynchronously from the controller/action method, I did not come across a solution which uses Task-based Asynchronous Pattern (TAP). The best one so far I saw was this. However it uses AsyncController instead of the new "async" pattern. Thus, I gave up googling around and came-up with the following approach: -
I am using ProDinner as an example base to demo my approach. Say the user is on the list of meals view (as shown below) and wants to check the deal at local grocery store for that item. (the check deal functioning is not foolproof). I have created a CodeActivity namely GetDeal which takes in ItemName and Zip as InArgument(s) and spits out IEnumerable<Deal> as OutArgument.
I've mentioned details on how I make the web api call using the new HttpClient within the AsyncCodeAcitivity in my next blog post.
I've mentioned details on how I make the web api call using the new HttpClient within the AsyncCodeAcitivity in my next blog post.
Here's the code snippet which demonstrates the invoking of workflow activity GetDeal -
public class DealController : Controller
{
//
// GET:
/Deal/
public async Task<ActionResult> Index(string id)
{
var wfInputArgs = new Dictionary<string, object>
{
{"ItemName", id},
//Hard Coding the zip for now
{"Zip", "07094"}
};
var wfOutputArgs = await
Task<IDictionary<string, object>>.Factory.StartNew(
() => WorkflowInvoker.Invoke(new GetDeal(), wfInputArgs));
var deals = wfOutputArgs["Deals"] as IEnumerable<Deal>;
var model = Mapper.Map<IEnumerable<Deal>, IEnumerable<DealModel>>(deals);
return PartialView(model);
}
}
Unlike WorkflowApplication, WorkflowInvoker.Invoke invokes a workflow synchronously. However, we schedule this as a new task using TaskFactory.StartNew.
Cheers!
No comments:
Post a Comment