AsyncController calling WCF webservice

Hi,

I am using MVC 2.0.
In one controller action I need to call a WCF Web service.  
However, I only call this service in one scenario in my controller action.

So the problem is, the pattern for using the AsyncController, forces the Async (begin) action to be a void method.
Whilst it is only the Completed (callback) method that returns an Action Result.

This is my syncrhonous code below. So you can see my action needs to return an Action result in several scenarios
before i get to to make the call to the WCF service.
public ActionResult Register(int customerId)
{
if(DoSomething())
{
   return RedirectToAction("MyOtherAction");
}

if(DoSomethingElse()
{
 var result = myWCFService.UpdateCustomer(customerId));
 return View("CompletedView");
}
return View("NotCompletedView");
}

public bool DoSomething()
{
 return false;
}

public bool DoSomethingElse()
{
 return true;
}

Open in new window

(Pseudo code!)

So how can I turn the conditional wcf call of this action into an Asynchronous call?  

I need to do the DoSomething work before, and potentially return an action result,
Then in some scenarios make the WCF call.

Thanks for your help.




LVL 1
MrKevorkianAsked:
Who is Participating?
 
Easwaran ParamasivamConnect With a Mentor Commented:
0
 
MrKevorkianAuthor Commented:
thanks. good links

i ended up doing this:
public ActionResult Register(int customerId)
{
if(DoSomething())
{
   return RedirectToAction("MyOtherAction");
}

if(DoSomethingElse()
{
[b]return RedirectToAction("CallMyServiceAsync")[/b]
}
return View("NotCompletedView");
}

public bool DoSomething()
{
 return false;
}

public bool DoSomethingElse()
{
 return true;
}

public void CallMyServiceAsync()
{

 AsyncManager.OutstandingOperations.Increment();
            myWCFService.UpdateCustomerCompleted += (sender, args) =>
                                                                                      {
                                                                                          AsyncManager.Parameters[
                                                                                              "response"] = args.Result;
                                                                                          AsyncManager.
                                                                                              OutstandingOperations.
                                                                                              Decrement();
                                                                                     };

            myWCFService.UpdateCustomer(customerId);
}


public ActionResult CallMyServiceCompleted(object response)
{

// do something with my response.

}

Open in new window

0
All Courses

From novice to tech pro — start learning today.