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()
{
var input = new Dictionary<string, object>();
input["ViewData"] = this.ViewData;
var userState = "BeginInvoke example";
var invoker = new WorkflowInvoker(HelloMvcDefinition);
Task workflowTask = Task.Factory.FromAsync<IDictionary<string, object>>(
invoker.BeginInvoke(input, WorkflowCompletedCallback, userState),
invoker.EndInvoke);
workflowTask.Wait();
return View();
}
I have tried this but I cannot seem to get it to work:
public async Task<ActionResult> Index()
{
var input = new Dictionary<string, object>();
input["ViewData"] = this.ViewData;
var userState = "BeginInvoke example";
var invoker = new WorkflowInvoker(HelloMvcDefinition);
Task workflowTask = Task.Factory.FromAsync<IDictionary<string, object>>(
invoker.BeginInvoke(input, WorkflowCompletedCallback, userState),
invoker.EndInvoke);
await workflowTask;
return View();
}
Unfortunately the view does not seem to work. Any ideas on what I am doing wrong?
EDIT After taking advice I have changed the method to this
public class HelloController : AsyncController
{
private static readonly HelloWorkflow HelloMvcDefinition = new HelloWorkflow();
public Task<ViewResult> Index()
{
var input = new Dictionary<string, object>();
input["ViewData"] = ViewData;
const string userState = "BeginInvoke example";
var invoker = new WorkflowInvoker(HelloMvcDefinition);
return Task.Factory.FromAsync<IDictionary<string, object>>(
invoker.BeginInvoke(input, WorkflowCompletedCallback, userState),
invoker.EndInvoke).ContinueWith(t => View());
}
static void WorkflowCompletedCallback(IAsyncResult result)
{
}
}
Which works fine so the problem must be how I am using the async keyword.
Thanks