使用.NET C#从azure云服务读取配置设置

时间:2017-04-19 07:10:09

标签: c# settings azure-cloud-services

我正在寻找一种使用C#从已部署的Windows Azure云服务中读取设置的方法。是否有可用的Azure SDK轻松加载此值?

Azure门户的屏幕截图,显示我想要阅读的设置:

enter image description here

[EDIT1]

我错过了补充说我尝试从外部应用程序加载设置,而不是从自己的服务加载。

1 个答案:

答案 0 :(得分:3)

根据您的说明,我假设您可以利用Microsoft Azure Management Libraries来检索配置设置,您可以按照以下步骤操作:

我创建了一个控制台应用程序并引用了 Microsoft Azure管理库,这是核心代码:

private static X509Certificate2 GetStoreCertificate(string thumbprint)
{
  List<StoreLocation> locations = new List<StoreLocation>
  { 
    StoreLocation.CurrentUser, 
    StoreLocation.LocalMachine
  };

  foreach (var location in locations)
  {
    X509Store store = new X509Store("My", location);
    try
    {
      store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
      X509Certificate2Collection certificates = store.Certificates.Find(
        X509FindType.FindByThumbprint, thumbprint, false);
      if (certificates.Count == 1)
      {
        return certificates[0];
      }
    }
    finally
    {
      store.Close();
    }
  }
  throw new ArgumentException(string.Format(
    "A Certificate with Thumbprint '{0}' could not be located.",
    thumbprint));
}
static void Main(string[] args)
{
    CertificateCloudCredentials credential = new CertificateCloudCredentials("{subscriptionId}", GetStoreCertificate("{thumbprint}"));
    using (var computeClient = new ComputeManagementClient(credential))
    {
        var result = computeClient.HostedServices.GetDetailed("{your-cloudservice-name}");
        var productionDeployment=result.Deployments.Where(d => d.DeploymentSlot == DeploymentSlot.Production).FirstOrDefault();
    }
    Console.WriteLine("press any key to exit...");
    Console.ReadKey();
}

您可以从productionDeployment.Configuration检索配置设置,如下所示:

enter image description here