应用应用程序设置之前,KeyVault ConfigurationBuilder会偶尔挂起

时间:2018-09-10 17:52:40

标签: c# visual-studio-2017 azure-keyvault

是否有人曾经体验过Azure.ConfigurationBuilder在启动过程中偶尔会挂起?我已将Azure KeyVault设置为连接服务,用于将开发人员验证到KeyVault中。然后,ConfigurationBuilder根据库中的秘密处理AppSettings的应用。

注意:这一切都很好,除非我连续调试,否则我可以成功导致ConfigurationBuilder挂起。没有产生错误。我已经使用ReSharpers dotTrace来查看挂断了什么:

enter image description here

除了利用ConfigurationBuilder外,我还在运行一些自定义项。我将ConnectionString存储在KeyVault中,并拉出它们并在PreApplicationStart上应用它们,同时允许ConfigurationBuilder处理AppSettings。

这是我的KeyVaultHandler,它使用ConnectedService对KeyVault进行身份验证:

public static class KeyVaultHandler
{
    private static readonly string VaultUri = ConfigurationManager.AppSettings["vaultUri"];
    private static readonly AzureServiceTokenProvider AzureServiceTokenProvider = new AzureServiceTokenProvider();
    private static readonly KeyVaultClient KvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(AzureServiceTokenProvider.KeyVaultTokenCallback));

    public static Dictionary<string, string> GetSecrets()
    {
        try
        {
            var result = KvClient.GetSecretsAsync(VaultUri).GetAwaiter().GetResult();
            return result.ToDictionary(value => value.Id, value => value.Identifier.Name);
        }
        catch (Exception e)
        {
            return null;
        }
    }

    public static string GetSecretValue(string key)
    {
        try
        {
            return KvClient.GetSecretAsync(key).GetAwaiter().GetResult().Value;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    public static string GetSecretType(string key)
    {
        try
        {
            return KvClient.GetSecretAsync(key).GetAwaiter().GetResult().ContentType;
        }
        catch (Exception e)
        {
            return null;
        }
    }
...

这是我的启动类,适用于我的ConnectionStrings:

[assembly: PreApplicationStartMethod(typeof(SettingsProcessor), "Start")]
namespace CompanyKeyVault
{
    public static class SettingsProcessor
    {
        #region Public Methods and Operators

        public static void Start()
        {
            var secrets = KeyVaultHandler.GetSecrets();
            foreach (var entry in secrets)
            {
                var name = entry.Value;
                var val = KeyVaultHandler.GetSecretValue(entry.Key);
                var type = KeyVaultHandler.GetSecretType(entry.Key);

                if (type != null)
                {
                    KeyVaultContentTypes enumType = (KeyVaultContentTypes)Enum.Parse(typeof(KeyVaultContentTypes), type);
                    switch (enumType)
                    {
                        case KeyVaultContentTypes.ConnectionString:

                            //Remove the KeyVault prefix in the name
                            name = Regex.Replace(name, @"Connection--", string.Empty);

                            //Now lets check if it is a Entity conecction or reguler SQL connection
                            string dbProvider = "System.Data.SqlClient";
                            Regex r3 = new Regex(@"App=EntityFramework");
                            Match m3 = r3.Match(val);
                            if (m3.Success)
                            {
                                dbProvider = "System.Data.EntityClient";
                            }

                            SetConnectionString(name, val, dbProvider);
                            break;
                    }
                }
            }
        }
...

我相当确定上述代码不是我提到的问题,因为在应用AppSettings之前ConfigurationBuilder挂起是我遇到的问题。在ConfigurationBuilder挂断之前,此代码还将正常触发,认证和提取所有KeyVault机密,而不会出现问题。上面的代码严格地适用于我的ConnectionString,正如我提到的,我将AppSettings留给ConfigurationBuilder来应用。这是我的web.config,涵盖了这一点:

  <configSections>
    <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
  </configSections>
  <configBuilders>
    <builders>
      <add name="AzureKeyVault" vaultName="my-key-vault" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=1.0.0.0, Culture=neutral" />
    </builders>
  </configBuilders>
...
  <appSettings configBuilders="AzureKeyVault">
...
  

如果有所作为,我还将Azure Redis缓存用作   SessionState提供程序。我不确定这是否会对   ConfigurationBuilder完成了,但是我想我会提到它。

我已经对此进行了广泛的搜索,但老实说,与Conneccted Services或ConfigurationBuilder相关的文档没有太多相关问题。

还需要注意的是,我正在运行 Asp.Net 4.7.1 WebForms应用程序 (这不是大多数文档都围绕着的Core应用程序)。我还构建了多个仅包含KeyVault逻辑的基本测试应用程序,并精简了这些应用程序,这些应用程序看起来更加一致,这使我相信大型应用程序中的某些内容会与ConfigurationBuilder混为一谈。

我想我的问题如下:

  1. 有人有过这种经历吗?
  2. 我应该在哪里寻找这种挂起的根本原因?
  3. 有人知道我可以应用的任何代码,这些代码会使ConfigurationBuilder超时或产生某种异常,以便在此处更好地隔离和识别问题吗?
  4. 欢迎提供任何启发性信息!

谢谢大家,很抱歉这个问题不是很详细-我想这就是我要发布的原因,因为我在确定问题/原因时遇到了麻烦,而且是零星的。

0 个答案:

没有答案