Click here to Skip to main content
14,392,000 members

How do I do async programming in webapi inside foreach in .NET 4.5

Charlie Andrews asked:

Open original thread
Hello ,
I am trying to create an asp.net web api. This API will be called from a scheduler from cloud once every 15 minute where this API will look in db and Call an external third party web-service multiple times with different parameters.External service will just return true of false.

I am fairly new to async programming. I don't even know what i am doing is right and if so , is it the correct way to do it. I have read many articles and gone through multiple same questions on stack overflow but couldn't find the answer.

Some article says it's not good to use task in webapi , some says use parallel prog some say don't use parallel prog on server. i am confused. Please tell me the correct way to use async prog at server.

What I have tried:

public void FindSessionAndInitiateCall()
       {
           using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
           {
               using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandType = CommandType.StoredProcedure;
                   cmd.CommandText = "sp_FindSessionsToConnect";
                   SqlDataReader dr = cmd.ExecuteReader();
                   while (dr.Read())
                   {
                       CreateRequest(dr["ManagerMob"].ToString(), dr["EmployeeMob"].ToString(), dr["SessionDuration"].ToString(), dr["SlotId"].ToString());
                   }
               }
           }
       }

       private async Task<string> CreateRequest( string ManagerMob,string EmployeeMob, string SessionDuration, string SlotId)
       {
           HttpWebRequest request;
           var result = "";
           try
           {
               string URL = "*******************";
               request = (HttpWebRequest)WebRequest.Create(URL);
               request.Method = "POST";
               WebResponse response = await request.GetResponseAsync();
               //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               using (StreamReader reader = new StreamReader(response.GetResponseStream()))
               {
                   var objText = reader.ReadToEnd();
                   var root = JObject.Parse(objText);
                   result = root["msg"].ToString();
               }
               response.Close();
               return result;
           }
           catch (Exception ex)
           {
               return ex.Message;
           }


       }
Tags: C#, Async, WebAPI, Await

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100