使用 .NET C# 从 Azure 云服务读取配置设置
Read configuration settings from azure cloud service with .NET C#
我正在寻找一种方法来使用 C# 从已部署的 windows Azure 云服务中读取设置。是否有任何 Azure SDK 可用于轻松加载此值?
来自 Azure 门户的屏幕截图显示了我想要阅读的设置:
[编辑1]
我想补充一点,我试图从外部应用程序而不是它自己的服务加载设置。
根据您的描述,我假设您可以利用 Microsoft Azure Management Libraries 来检索配置设置,您可以按照以下步骤操作:
将证书导出到.cer 文件并上传到您的 azure 订阅,有关更多详细信息,您可以按照此 tutorial
我创建了一个控制台应用程序并引用了 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
中检索配置设置,如下所示:
我正在寻找一种方法来使用 C# 从已部署的 windows Azure 云服务中读取设置。是否有任何 Azure SDK 可用于轻松加载此值?
来自 Azure 门户的屏幕截图显示了我想要阅读的设置:
[编辑1]
我想补充一点,我试图从外部应用程序而不是它自己的服务加载设置。
根据您的描述,我假设您可以利用 Microsoft Azure Management Libraries 来检索配置设置,您可以按照以下步骤操作:
将证书导出到.cer 文件并上传到您的 azure 订阅,有关更多详细信息,您可以按照此 tutorial
我创建了一个控制台应用程序并引用了 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
中检索配置设置,如下所示: