How can 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.

  • Add the web link for the web service to the project.

  • 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 โ€œAsynchronous operation cannot be started at this time. Asynchronous operations can only be started in an asynchronous handler or module or during certain events in the page life cycle. If this exception occurs when When the page is running, make sure the page is marked <% @Page Async = "true"%>. "

To overcome this problem, 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 call to the Wservice_login_completed () function was called by returning View ***, so I am not getting the result. How to achieve a "web service call from a controller." Any ideas?

+4
source share
3 answers

Finally, I successfully called the web service from the MVC controller.

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 the endpoint for our service .. you can get the endpoint, which will be presented in the app.confiq or web.config files. Happy coding ...!

+2
source
  • get the following NuGet:

     microsoft http client (id = Microsoft.Net.Http) 
  • Create a web controller (webapi_Controller_Name)
    Your Post function should look like the following function
    Put this feature in your Api web controller

     [HttpPost] public void PostForm(objUser ws_Obj) { // put you code here } 
  • Call your web service from your regular controller as follows. This is an Async call and the web service will return immediately.

     //call the web service, Asynch HttpClient client = new HttpClient(); client.BaseAddress = new Uri("52323/"); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.PostAsJsonAsync("//52323/api/webapi_Controller_Name/PostForm", objContact); 
0
source

First, create a service link by right-clicking on the project name in the solution explorer, then hover over the "Add" parameter and click on "Service Link ..."

Secondly, paste the web service address in the "Address" field on the "Add a service link" page, be sure to add "? Wsdl" to the end of your web service or it will not work, and then click "Go". You will see that the web service is displayed in the Services area. Click on a service to view the available services that appear in the Operations section. Rename the service if you want, then click OK to create the service.

Finally, put the following code in your MVC controller. Put the code in the Get or Post controller, it doesn't matter.

  // Declare the Request object. ServiceReference1.GetSurveyRequest myGSRq = new ServiceReference1.GetSurveyRequest(); // You can set the webservice parameters here like... myGSRq.Address = getUserData[0].address; myGSRq.City = getUserData[0].city; myGSRq.State = getUserData[0].state; // Now declare the Response object. ServiceReference1.GetSurveyResponse myGSR = new ServiceReference1.GetSurveyResponse(); //And then use the following to process the request and response. ServiceReference1.EMPortTypeClient emptc = new ServiceReference1.EMPortTypeClient(); myGSR = emptc.GetSurvey(myGSRq); // In this example the response Object comes back with a URL used to redirect the user //after the webservice has been processed. if (myGSR.Url != null) Response.Redirect(myGSR.Url.ToString()); else Response.Write("Error"); return null; 

Pretty simple, hope this helps!

If you are creating a new service and are able to use a web service or a web API, I suggest using a web API. Build a RESTful API using ASP.NET Web APIs

0
source

Source: https://habr.com/ru/post/1486388/


All Articles