locked
Start consuming different services the same time RRS feed

  • Question

  • User1897897189 posted

    Dears,

    I have 10 different web services(some are wsdls and some apis)
    to be displayed on a web page.

    Some services retrieves data very fast some are too slow.

    I want to start consuming all the 10 services same time.

    Is it possible to do it?

    Please provide me some samples.

    Thanks
    Nick

    Sunday, March 31, 2019 3:27 PM

All replies

  • User-1174608757 posted

    Hi ,nicklibee

    According to your description,If you want to call services in code behind, I suggest you to use send async method, so that you could send request to multiply services at same time. However ,even if you use send async, the program still needs time to get the responses from all the services so that the page could show. If some of the services is too slow , you still have to wait it finished.

    Else , in front end I suggest that you could use ajax for async request.Ajax will call async request by default. Here is a demo,I hope it could help you.

    Using ajax:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>  
    <!DOCTYPE html>  
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head runat="server">  
        <title></title>  
        <script src="jquery-1.7.1.js" type="text/javascript"></script>  
         <script>  
             $(document).ready(function () {  
                 $("#Save").click(function () {  
                     var person = new Object();  
                     person.name = $('#name').val();  
                     person.surname = $('#surname').val();  
                     $.ajax({  
                         url: 'http://localhost:3413/api/person',  
                         type: 'POST',  
                         dataType: 'json',  
                         data: person,  
                         success: function (data, textStatus, xhr) {  
                             console.log(data);  
                         },  
                         error: function (xhr, textStatus, errorThrown) {  
                             console.log('Error in Operation');  
                         }  
                     });  
                 });  
             });  
        </script>  
    </head>  
    <body>  
        <form id="form1">  
            Name :- <input type="text" name="name" id="name" />  
            Surname:- <input type="text" name="surname" id="surname" />  
            <input type="button" id="Save" value="Save Data" />  
        </form>  
    </body>  
    </html>  

    create the class for Web API 

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Net;  
    using System.Net.Http;  
    using System.Web.Http;  
    namespace WebApplication1.WebAPI  
    {  
        public class person  
        {  
            public string name { get; set; }  
            public string surname { get; set; }  
        }  
        public class personController : ApiController  
        {  
            [HttpPost]  
            public string Post( person obj)  
            {  
                return obj.name + obj.surname;  
            }  
       }  
    }  

    Then we could see 

    If you want to use async in code behind. you could follow the steps in the link as below

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45

    Best Regards

    Wei

    Monday, April 1, 2019 2:57 AM