locked
How to consume multiple web services at the same time in MVC? RRS feed

  • Question

  • User1897897189 posted

    Dears,

    How to consume multiple web services at the same time in MVC?

    Is it possible to create a user control in MVC ?

    If answers to above questions are yes, please provide me a sample.

    Following is the working code of a asp.net system.

    public async Task datconsum(){

    var a = task.run(() => usercontrol1.details(input));
    var b = task.run(() => usercontrol2.details(input));

    await.task.whenall(a,b);

    }

    What I want to do is
    Create a user control.

    Pass the input parameter.

    Consume a web service.

    Bind the data in the control itself.


    Thanks
    Nick

    Sunday, July 5, 2020 4:28 PM

Answers

  • User1686398519 posted

    Hi nicklibee,

    • You can use multiple web services.
      • For how to add web services, please refer to the following operations.

      • For how to use multiple web services, please refer to the example.
    • You don't need to create user control in MVC.
      • When passing parameters in mvc, you only need to ensure that the parameter names are the same.Please refer to the example for specific usage.
      • For more information about model binding in MVC, please refer to this link.

    WebService1.asmx(WebService2.asmx is similar to WebService1.asmx, so only WebService1.asmx is given.)

        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        public class WebService1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string test1(string test1value)
            {
                return test1value;
            }
        }

    Controller

            public ActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public async Task<ActionResult> IndexAsync(string test1value,string test2value)
            {
                WebService1 test1 = new WebService1();
                WebService2 test2 = new WebService2();
                var a = Task.Run(()=> test1.test1(test1value));
                var b = Task.Run(() => test2.test2(test2value));
                await Task.WhenAll(a,b);
                return View();
            }

    Index

    @using (Html.BeginForm("IndexAsync", "Test", FormMethod.Post))
    {
        @Html.TextBox("test1value")
        @Html.TextBox("test2value")
        <button type="submit">submit</button>
    }

    Here is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 6, 2020 12:06 PM

All replies

  • User475983607 posted

    How to consume multiple web services at the same time in MVC?

    Sure.  How depends on the service you are consuming; REST or SOAP.  If SOAP created a service reference.  If REST use the standard HttpClient.

    Is it possible to create a user control in MVC ?

    MVC does not have user controls.

    I recommend going through a few Getting Started tutorials. 

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started

    Sunday, July 5, 2020 5:05 PM
  • User-474980206 posted

    Also there is no need for Task.Run, webclient is async. As MVC doesn’t have user controls, you might want to upgrade to Razor pages which does hove components.

    Sunday, July 5, 2020 6:10 PM
  • User1686398519 posted

    Hi nicklibee,

    • You can use multiple web services.
      • For how to add web services, please refer to the following operations.

      • For how to use multiple web services, please refer to the example.
    • You don't need to create user control in MVC.
      • When passing parameters in mvc, you only need to ensure that the parameter names are the same.Please refer to the example for specific usage.
      • For more information about model binding in MVC, please refer to this link.

    WebService1.asmx(WebService2.asmx is similar to WebService1.asmx, so only WebService1.asmx is given.)

        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        public class WebService1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string test1(string test1value)
            {
                return test1value;
            }
        }

    Controller

            public ActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public async Task<ActionResult> IndexAsync(string test1value,string test2value)
            {
                WebService1 test1 = new WebService1();
                WebService2 test2 = new WebService2();
                var a = Task.Run(()=> test1.test1(test1value));
                var b = Task.Run(() => test2.test2(test2value));
                await Task.WhenAll(a,b);
                return View();
            }

    Index

    @using (Html.BeginForm("IndexAsync", "Test", FormMethod.Post))
    {
        @Html.TextBox("test1value")
        @Html.TextBox("test2value")
        <button type="submit">submit</button>
    }

    Here is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 6, 2020 12:06 PM
  • User1897897189 posted

    Thanks the async task is working perfect.

    I have one more doubt.

    Is it possible to bind the webservices output to a partial view without coming back to the async function?


    What I mean is,after executing the IndexAsync function (var a = Task.Run(()=> test1.test1(test1value))),
    control goes to another controller WebService1, consume the web service and without binding the data ,
    control return back to the IndexAsync function.

    So I cant see the web services info.

    Can you please help me to sort out this?

    Thanks

    Nick

    Saturday, August 1, 2020 7:05 AM