以编程方式访问

可以通过编程方式访问通知中心遥测数据,类似于使用前面表中提供的 REST 标识符 (Microsoft Azure 服务总线指标来访问相应的指标) 。

步骤 1:创建证书

首先,创建一个证书以便访问 Azure 订阅资源。 在 Windows 中,执行以下操作:

  1. 打开 Visual Studio 管理员命令提示符,并键入以下命令:

    makecert -sky exchange -r -n "CN=<CertificateName>" -pe -a sha1 -len 2048 -ss My "<CertificateName>.cer"
    
  2. 运行 Certmgr.msc,单击左侧的“个人”,然后右键单击你创建的证书,单击“所有任务”,然后单击“导出”

    Portal

  3. 按照向导进行操作,并选择不导出私钥的选项。 选择导出 CER 证书的选项,然后提供以 .cer 结尾的文件名。

    Portal

  4. 重复执行导出过程,这次选择导出 PFX 文件中的私钥。 然后选择以 .PFX 结尾的名称。

步骤 2:将证书Upload到 Azure

现在上载你的 .CER 文件,使证书能够对 Azure 资源执行操作。

  1. 在 Azure 管理门户中,单击左侧的“设置”,然后单击“管理证书”

  2. 单击屏幕底部的“上载”,然后选择你的 .CER 文件。

  3. 记下要管理的订阅 ID。

    注意

    订阅 ID 必须用于包含通知中心的订阅。

    Portal

步骤 3:通过 REST 接口访问指标

若要读取遥测数据,必须使用上一节) 中报告的指标名称,对根据Microsoft Azure 服务总线指标中指定的规则构造的 URL 发出 REST 调用 (。

以下代码是检索自 2013-08-06T21:30:00Z 后的 5 分钟间隔内聚合的成功推送数的示例(请记住将 subscriptionId、命名空间名称、通知中心名称和 pfx 证书路径替换为你的值)。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace telemetry1
{
    class Program
    {
        [DataContract(Name = "properties", Namespace = "https://schemas.microsoft.com/ado/2007/08/dataservices")]
        public class MetricValue
        {
            [DataMember(Name = "Timestamp")]
            public DateTime Timestamp { get; set; }

            [DataMember(Name = "Min")]
            public long Min { get; set; }

            [DataMember(Name = "Max")]
            public long Max { get; set; }

            [DataMember(Name = "Total")]
            public long Total { get; set; }

            [DataMember(Name = "Average")]
            public float Average { get; set; }
        }

        static void Main(string[] args)
        {
            string uri = @"https://management.core.windows.net/{subscriptionId}/services/ServiceBus/namespaces/{namespaceName}/NotificationHubs/{hubName}/metrics/outgoing.allpns.success/rollups/PT5M/Values?$filter=Timestamp%20gt%20datetime'2014-08-06T21:30:00Z'";
            HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(uri);
            sendNotificationRequest.Method = "GET";
            sendNotificationRequest.ContentType = "application/xml";
            sendNotificationRequest.Headers.Add("x-ms-version", "2015-01");
            X509Certificate2 certificate = new X509Certificate2(@"{pathToPfxCert}", "{certPassword}");
            sendNotificationRequest.ClientCertificates.Add(certificate);


            try
            {
                HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();

                using (XmlReader reader = XmlReader.Create(response.GetResponseStream(), new XmlReaderSettings { CloseInput = true }))
                {
                    SyndicationFeed feed = SyndicationFeed.Load<SyndicationFeed>(reader);

                    foreach (SyndicationItem item in feed.Items)
                    {
                        XmlSyndicationContent syndicationContent = item.Content as XmlSyndicationContent;
                        MetricValue value = syndicationContent.ReadContent<MetricValue>();
                        Console.WriteLine(value.Total);
                    }
                }
            }
            catch (WebException exception)
            {
                string error = new StreamReader(exception.Response.GetResponseStream()).ReadToEnd();
                Console.WriteLine(error);
            }
        }


    }
}