Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i call webservice from MVC3 Razor Controller?

In my project I need to call a web service from a controller. I have already done the following, and it works.

  1. Add the web reference for the web service to the project.

  2. Call the service as follows:

    Service Wservice=new Service();
    Wservice.loginCompleted+=new Wservice_login_Completed;
    WService.login_Async("username","Password");
    

    Note: Whenever i call this service it throws an error that is "An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>."

To overcome this issue I use

 [Httppost]
 public ActionResult login(logmodel model)
 {
   Task.Factory.StartNew(() => 
    { 
    Wservice.loginCompleted+=new Wservice_login_Completed;
    WService.login_Async("username","Password");
    });

    if(finalresult==true)
    {
      *** return View();
    }
  }

  void Wservice_login_completed()
  {
      Here i got the output.
  }

But the calling of Wservice_login_completed() function was after the View*** was returned,, so I'm not getting the result. How do I achieve "calling webservice from Controller".. Any ideas?

like image 347
Jasper Manickaraj Avatar asked Nov 03 '22 20:11

Jasper Manickaraj


1 Answers

Finally i called the webservice from MVC Controller Successfully.

Note: Add ServiceReference instead of WebReference and avoid
"Task.Factory.StartNew(()=>);" Process.

  [Httppost]
 public ActionResult login(logmodel model)
 {
    Wservice.ServiceSoapClient _host = new Wservice.ServiceSoapClient("ServiceSoap");

    var result_out = _host.login(uname, pwd, "test1", "test2", "test3", "test4");
 }

Here "ServiceSoap" is an endpoint for our service.. you may got the endpoint to be presented in app.confiq or web.config files. Happy Coding...!

like image 79
Jasper Manickaraj Avatar answered Nov 15 '22 08:11

Jasper Manickaraj