Async ctp MVC4 and workflows

前端 未结 2 1582
梦如初夏
梦如初夏 2021-01-28 10:01

I have the below code which calls a workflow. I am wondering if I can changed the controller to be asynchronous using the async ctp.

public ActionResult Index()
         


        
相关标签:
2条回答
  • 2021-01-28 10:40

    We can use the TAP pattern for invoking windows workflow as follows - (Details are mentioned in my blog post http://tweetycodingxp.blogspot.com/2013/06/invoke-workflow-wf-using-task-based.html)

    public async Task<ActionResult> Index(string id)
    {
        var wfInputArgs = new Dictionary<string, object>
        {
            ...
        };
    
        var wfOutputArgs = await Task<IDictionary<string, object>>.Factory.StartNew(
            () => WorkflowInvoker.Invoke(new MyWorkflow(), wfInputArgs));
        var result = wfOutputArgs["Output1"] as IEnumerable<Class1>;
        ...
        return View(model);
    }
    
    0 讨论(0)
  • 2021-01-28 10:47

    Derive from AsyncController instead of Controller.

    EDIT: You may also be experiencing a known bug where ASP.NET MVC4 will hang if the action returns a completed Task. You can work around this bug by adding await Task.Yield(); at the top of the action method.

    On an unrelated note, this code is more efficient (and shorter, too):

    var workflowTask = Task.Factory.FromAsync(invoker.BeginInvoke, invoker.EndInvoke,
        input, userState);
    
    0 讨论(0)
提交回复
热议问题