在 .Net framework 4.5 中引入了 async 和 await 关键字来进行异步调用。
我也在 Web 应用程序中使用过它们。我开始知道它也可以使用代理来完成。
下面是我的示例片段,展示了异步调用是如何完成的
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;
}
}
同样,上述两种方法也有相同的列表。
检索 11+19+6 条记录的数据大约需要 12-15 秒。
你们能帮我优化这段代码,这样它会更快。