使用.NET C#从Azure云服务中读取配置设置 [英] Read configuration settings from azure cloud service with .NET C#
问题描述
我正在寻找一种使用C#从已部署的Windows Azure云服务中读取设置的方法.是否有可用的Azure SDK可以轻松加载此值?
I am searching for a way to read settings from a deployed windows azure cloud service with C#. Is there any Azure SDK available to easily load this values?
Azure门户的屏幕截图,以显示我要阅读的设置:
Screenshot from Azure Portal to show which settings i want to read:
我错过了补充,我尝试从外部应用程序而不是从其自身的服务中加载设置.
I missed to add that I tried to load the settings from an external application and not from the service it self.
推荐答案
According to your description, I assumed that you could leverage Microsoft Azure Management Libraries to retrieve the configuration settings, you could follow the steps below:
-
将证书导出到.cer文件并将其上传到Azure订阅,有关更多详细信息,您可以遵循以下
Export the cert to a .cer file and upload it to your azure subscription, for more details, you could follow this tutorial
我创建了一个控制台应用程序,并引用了 Microsoft Azure管理库,这是核心代码:
I created a console application and reference the Microsoft Azure Management Libraries, here is the core code:
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
检索配置设置,如下所示:You could retrieve the configuration settings from
productionDeployment.Configuration
as follows:这篇关于使用.NET C#从Azure云服务中读取配置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!