使用 MVC 列出集群(页面持续加载数小时)

Listing clusters using MVC (the page keeps loading for hours)

使用 "Loggedin" 视图我试图在我的订阅中列出 HDInsight 集群,但是当我尝试 运行 代码时,"Loggedin" 页面一直在加载,但没有任何 output.I我不知道我哪里错了。

调试时,代码在控制器中的 var cluster = client.ListClusters(); 行挂起。 在这种情况下,输出 window 显示以下消息:

Microsoft.WindowsAzure.ServiceRuntime Verbose: 500 : Role instance status check starting Microsoft.WindowsAzure.ServiceRuntime Verbose: 502 : Role instance status check succeeded: Ready The thread 0x59fc has exited with code 259 (0x103).

如有任何帮助,我们将不胜感激。

控制器

 public ActionResult Loggedin()
    {

        Guid subscriptionId = new Guid("Subscription_id");     //your-subscription-id
        string certName = "Friendly_name";                     //your friendly name

        // Create an HDInsight Client
        X509Store store = new X509Store(StoreName.My);
        store.Open(OpenFlags.ReadOnly);
        X509Certificate2 cert = store.Certificates.Cast<X509Certificate2>().First(item => item.FriendlyName == certName); 
        HDInsightCertificateCredential creds = new HDInsightCertificateCredential(subscriptionId, cert);
        IHDInsightClient client = HDInsightClient.Connect(creds);


         var cluster = client.ListClusters();

         var c = new List<ClusterList>();

         foreach (var item in cluster)
         {
             c.Add(new ClusterList() { Name = item.Name });
             c.Add(new ClusterList() { Node = item.ClusterSizeInNodes });
         }

          return View(c);

    }

型号

public class ClusterList
{
    public string Name { get; set; }
    public int Node { get; set; }
}

查看

 @model List<listCluster.Models.ClusterList>
@{
    ViewBag.Title = "Loggedin";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

    <h2>Loggedin</h2>
<div>
 
    <table>
        <tr>
            <th>@Html.Label("Name")</th>
            <th>@Html.Label("Node")</th>
        </tr>
        <tr>
            <td>@Model.Name</td>
            <td>@Model.Node</td>
        </tr>
    </table>

</div>

thread exit code 上的 post 之一帮助我找到了确切的问题。 此外 post Stephen Cleary - Don't Block on Async Code 帮助我解决了问题。

代码中的以下更改解决了问题:

控制器

 public async Task<ActionResult> Loggedin()
    {
        ViewBag.Message = "Your application description page.";
        var c = new List<ClusterList>();
        var call = Task.Factory.StartNew(() => c=GetAsync());

        await call;
        return View(c);
    }


    public List<ClusterList> GetAsync()
    {
        Guid subscriptionId = new Guid("your-subscription-id");     //your-subscription-id
        string certName = "Friendly";//your-subscription-management-cert-name

        // Create an HDInsight Client
        X509Store store = new X509Store(StoreName.My);
        store.Open(OpenFlags.ReadOnly);
        X509Certificate2 cert = store.Certificates.Cast<X509Certificate2>().Single(item => item.FriendlyName == certName); //your friendly name
        HDInsightCertificateCredential creds = new HDInsightCertificateCredential(subscriptionId, cert);
        IHDInsightClient client = HDInsightClient.Connect(creds);


        var cluster = client.ListClusters();

        var c1 = new List<ClusterList>();
        foreach(var item in cluster)
        {
            c1.Add(new ClusterList() { Name = item.Name, Node = item.ClusterSizeInNodes });
        }
        return c1;
    }

模型保持不变。

查看

@model List<listCluster.ClusterList>

@{
ViewBag.Title = "Loggedin";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Loggedin</h2>

<div>

    <table>
        <tr>
            <th>@Html.Label("Name")</th>
            <th>@Html.Label("Node")</th>
        </tr>
       
            @foreach(var item in Model)
              {
                <tr>
                    <td>@item.Name</td>
                    <td align="center">@item.Node</td>
                    

                </tr>
              }


    </table>

</div>