azure - Asynchronous calls using Async and Await -
in .net framework 4.5 async , await keywords introduced async calls.
i have used them in web applications too. came know can done using doing delegates.
below sample snippet showing how async calls done
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 empty, means affinity grouping returned, pull location affinity grouping 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);** method definition 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; seek { webresponse = (httpwebresponse)await request.getresponseasync(); } grab (exception) { } httpstatuscode statuscode = webresponse.statuscode; if (webresponse.contentlength > 0) { using (xmlreader reader =xmlreader.create(webresponse.getresponsestream())) { responsebody.load(reader); } } if (statuscode.equals(httpstatuscode.ok)) { homecoming responsebody; } else { homecoming null; } }
similarly above 2 methods have same kind of listing.
its taking me approximately 12-15 seconds retrieve info of 11+19+6 records.
could guys help me in optimizing code much faster.
first need profile code see loosing time. if getxmldocument
taking of time may server not responsive
outside of guess foreach loop taking amount of time because searching through elements in each statement in here.
another way of doing
dictionary<string, string> nvpairsforcolumns = new dictionary { "url", string.empty }; // add together 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 not valid entry else maintain dictionary<string,bool> tag when done first { nvpairsforcolumns[xelement.name] = xelement.innertext; } }
azure delegates xml-parsing sql-azure async-await
No comments:
Post a Comment