.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;
}
}
同様に、上記の2つのメソッドにも同じ種類のリストがあります。
11 + 19 + 6レコードのデータを取得するのに約12〜15秒かかります。
このコードを最適化して、はるかに高速になるようにお手伝いしていただけませんか。