0

Dans .Net framework 4.5, les mots-clés async et await sont introduits pour effectuer des appels asynchrones.Les appels asynchrones utilisant Async et Await

Je les ai aussi utilisées dans des applications Web. Je suis venu à savoir que cela peut aussi être fait en faisant des délégués.

Voici mon extrait de l'échantillon montrant comment les appels sont effectués async

   Public void binddata() 
       { 
       certificate = HelperMethods.GetStoreCertifcate(Thumbprint); 
       ListHostedServices(SubscriptionId, certificate, Version); 
       hostedservicesview.ActiveViewIndex = 0; 
       ListStorageAccounts(SubscriptionId, certificate, Version); 
       } 

public async void ListHostedServices(string subscriptionId, X509Certificate2 certificate, string version) 
     { 

      string hittingUri = String.Format("https://management.core.windows.net/{0}/" + "services/hostedservices",SubscriptionId); 
      XmlDocument responsebody= await HelperMethods.GetXmlDocument(hittingUri, certificate, version); 

      if (responsebody != null) 
      { 
       var result = responsebody.GetElementsByTagName("HostedServiceProperties"); 



       hostedservices = new DataTable(); 
       hostedservices.Columns.Add("Url"); 
       hostedservices.Columns.Add("ServiceName"); 

       hostedservices.Columns.Add("Location"); 
       hostedservices.Columns.Add("Label"); 
       hostedservices.Columns.Add("Status"); 
       hostedservices.Columns.Add("DateCreated"); 
       hostedservices.Columns.Add("DateLastModified"); 
       foreach (XmlNode hsnode in result) 
       { 
        DataRow hsrow = hostedservices.NewRow(); 
        hsrow["Url"] = hsnode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Url").Any() ? 
          hsnode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Url").First().InnerText : string.Empty; 
        hsrow["ServiceName"] = hsnode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "ServiceName").Any() ? 
          hsnode.ParentNode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "ServiceName").First().InnerText : string.Empty; 
        hsrow["Location"] = hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Location").Any() ? 
          hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Location").First().InnerText : string.Empty; 

        // IF location is empty, it means affinity group is returned, Pull location from affinity group 
        if (String.IsNullOrEmpty(hsrow["Location"].ToString())) 
        { 
         string affnitygroup = hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "AffinityGroup").Any() ? 
          hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "AffinityGroup").First().InnerText : string.Empty; 

         certificate = HelperMethods.GetStoreCertifcate(Thumbprint); 
         hsrow["Location"] = await HelperMethods.GetAffinityGroupLocation(subscriptionId, certificate, Version, affnitygroup); 


        } 
        hsrow["Label"] = hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Label").Any() ? 
          hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Label").First().InnerText : string.Empty; 
        hsrow["Status"] = hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Status").Any() ? 
         hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Status").First().InnerText : string.Empty; 
        hsrow["DateCreated"] = hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "DateCreated").Any() ? 
         hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "DateCreated").First().InnerText : string.Empty; 
        hsrow["DateLastModified"] = hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "DateLastModified").Any() ? 
         hsnode.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "DateLastModified").First().InnerText : string.Empty; 
        hostedservices.Rows.Add(hsrow); 

       } 
       lbl_count.Text = hostedservices.Rows.Count.ToString(); 
       HostedServicesList.DataSource = hostedservices; 
       HostedServicesList.DataBind(); 

      } 

      else 
      { 

      } 




     } 

**XmlDocument responsebody= await HelperMethods.GetXmlDocument(hittingUri, certificate, version);** 
The method definition is as follows 
public static async Task<XmlDocument> GetXmlDocument(string hittingUrl, X509Certificate2 certificate, string Version) 
     { 
      HttpWebRequest request; 
       XmlDocument responsebody = new XmlDocument(); 
      // string hittingUri = "https://management.core.windows.net/{0}/" + "services/hostedservices"; 
      Uri uri = new Uri(hittingUrl); 


      request = (HttpWebRequest)HttpWebRequest.Create(uri); 

      request.Method = "GET"; 
      request.Headers.Add("x-ms-version", Version); 

      request.ClientCertificates.Add(certificate); 
      request.ContentType = "application/xml"; 




      HttpWebResponse webresponse= null; 

      try 
      { 
       webresponse = (HttpWebResponse)await request.GetResponseAsync(); 

      } 
      catch (Exception) 
      { 

      } 

      HttpStatusCode statuscode = webresponse.StatusCode; 
      if (webresponse.ContentLength > 0) 
      { 
       using (XmlReader reader =XmlReader.Create(webresponse.GetResponseStream())) 
       { 
        responsebody.Load(reader); 


       } 
      } 

      if (statuscode.Equals(HttpStatusCode.OK)) 
      { 
       return responsebody; 
      } 
      else 
      { 
       return null; 
      } 


     } 

De même au-dessus 2 méthodes ont aussi même genre de liste.

Cela me prend environ 12-15 secondes pour récupérer des données de 11 + 19 + 6 enregistrements.

Pourriez-vous m'aider à optimiser ce code afin qu'il soit beaucoup plus rapide.

Répondre

0

D'abord, vous devez profiler votre code pour voir où vous perdez votre temps. Si le GetXMLDocument prend la plupart du temps, il se peut que le serveur ne soit pas aussi réactif

En dehors de cela, je suppose que votre boucle foreach prend le plus de temps parce que vous recherchez essentiellement dans tous les éléments dans chaque déclaration ici.

Une autre façon de faire cela pourrait être

Dictionary<string, string> nvpairsForColumns = new Dictionary { "Url", String.Empty }; // add all valid column headers here 
foreach(var xelement in hsnode.ParentNode.ChildNodes.OfType<XmlElement>) 
{ 
    if(nvparisForColumns.ContainsKey(xelement.Name) 
    && String.IsNullOrEmpty(nvpairsForColumns[xlement.Name])) // assumption String.Empty is not a valid entry else keep another Dictionary<string,bool> to tag when done with first 
    { 
    nvpairsForColumns[xelement.Name] = xelement.InnerText; 
    } 

}