如何在运行时从Web服务内部获取CloudConfiguration的完整列表? [英] How to get full list of CloudConfiguration from inside a web service at runtime?

查看:126
本文介绍了如何在运行时从Web服务内部获取CloudConfiguration的完整列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ConfigurationManager具有AppSettings名称-值集合,但是CloudConfigurationManager仅具有GetSetting(string)方法,如果您知道密钥,则可以按1逐个获取配置设置.

ConfigurationManager has AppSettings name-value collection but CloudConfigurationManager has only GetSetting(string) method where you can get the config settings 1 by 1 if you know the key.

是否可以获取角色运行时的整个配置?

Is there a way to get the whole config of the role runtime?

根本原因是我想进行强类型化配置,以将其抽象化并使我的代码更具可测试性.直接使用CloudConfigurationManager是隐式依赖关系,我想使用要在测试中存根的抽象将其删除.所以我觉得这很实用.这使我想到了这个问题.

The root cause is that I want to make strong typed configuration in order to abstract it away and make my code more testable. Using CloudConfigurationManager directly is implicit dependency which I want to remove with an abstraction which I want to stub in tests. So I find this practical. Which brings me to my question.

我不想使用fx.configuration.azure之类的库,因为由于它需要继承基类,因此我将不得不完全携带其依赖项.

I do not want to use library like fx.configuration.azure because I will have to carry its dependency altogether because it requires inheritance of a base class.

推荐答案

AFAIK,没有直接的方法可以为您提供此信息.

AFAIK, there's no direct method available which will give you this information.

但是,您可以使用一种解决方法.它涉及使用服务管理API的 Get Deployment 操作.此操作将返回XML,并且其中一个元素Configuration包含Base64编码格式的服务配置文件.您可以阅读此元素,将其转换为字符串,然后解析XML以获取ConfigurationSettings元素.它的子元素包含所有设置.

However there's a workaround that you can use. It involves making use of Service Management API's Get Deployment operation. This operation will return an XML and one of the element there is Configuration which contains your service configuration file in Base64 encoded format. You can read this element, convert it into string and parse the XML to get to ConfigurationSettings elements. It's child elements contains all the settings.

为此,您可以通过Service Management REST API编写自己的包装器,也可以使用 Azure Management Library .

For this, you could either write your own wrapper over Service Management REST API or make use of Azure Management Library.

更新

因此,这里是一个示例代码,用于使用列出Service Configuration File中的所有配置设置. Azure Management Library .这是一个简单的控制台应用程序,可在很短的时间内被黑客入侵,因此有很大的改进空间:).对于管理证书,我使用了发布设置文件"中的数据.

So here's a sample code for listing all configuration settings from Service Configuration File using Azure Management Library. It's a simple console app hacked together in very short amount of time thus has a lot of scope of improvement :). For management certificate, I have used the data from Publish Setting File.

您只需要在 Azure Management Library Nuget软件包中安装您的控制台应用程序:

You just have to install Azure Management Library Nuget Package in your console application:

安装软件包Microsoft.WindowsAzure.Management.Libraries

Install-Package Microsoft.WindowsAzure.Management.Libraries

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Management.Compute;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;

namespace ReadConfigurationSettingsUsingAzureManagementLibrary
{
    class Program
    {
        static string subscriptionId = "<subscription-id>";
        static string managementCertContents = "<Base64 Encoded Management Certificate String from Publish Setting File>";//Certificate string from Azure Publish Settings file
        static string cloudServiceName = "<your cloud service name>";
        static string ns = "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration";
        static void Main(string[] args)
        {
            var managementCetificate = new X509Certificate2(Convert.FromBase64String(managementCertContents));
            var credentials = new CertificateCloudCredentials(subscriptionId, managementCetificate);

            var computeManagementClient = new ComputeManagementClient(credentials);
            var response = computeManagementClient.HostedServices.GetDetailed(cloudServiceName);
            var deployment = response.Deployments.FirstOrDefault(d => d.DeploymentSlot == Microsoft.WindowsAzure.Management.Compute.Models.DeploymentSlot.Production);
            if (deployment != null)
            {
                var config = deployment.Configuration;
                XElement configXml = XElement.Parse(config);
                var roles = configXml.Descendants(XName.Get("Role", ns));
                foreach (var role in roles)
                {
                    Console.WriteLine(role.Attribute("name").Value);
                    Console.WriteLine("-----------------------------");
                    var configurationSettings = role.Element(XName.Get("ConfigurationSettings", ns));
                    foreach (var element in configurationSettings.Elements(XName.Get("Setting", ns)))
                    {
                        var settingName = element.Attribute("name").Value;
                        var settingValue = element.Attribute("value").Value;
                        Console.WriteLine(string.Format("{0} = {1}", settingName, settingValue));
                    }
                    Console.WriteLine("==========================================");
                }
            }
            Console.ReadLine();
        }
    }
}

这篇关于如何在运行时从Web服务内部获取CloudConfiguration的完整列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆