コード例 #1
0
 public ConfigurConfigurationProvider
 (
     string appId,
     string appSecret,
     string appPassword,
     ConfigurOptions configurOptions,
     HttpClient httpClient
 )
 {
     _appId           = appId;
     _appSecret       = appSecret;
     _appPassword     = appPassword;
     _configurOptions = configurOptions;
     _httpClient      = httpClient;
 }
コード例 #2
0
        public static IConfigurationBuilder AddConfigur
        (
            this IConfigurationBuilder extended,
            IConfigurationRoot configuration,
            string connectionString,
            Action <ConfigurOptions> options
        )
        {
            SerilogLogger.Instance.Warning
            (
                "Adding Configur to IConfigurationBuilder."
            );

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                SerilogLogger.Instance.Warning
                (
                    "Failed to add Configur because the connection string is null or empty."
                );

                return(extended);
            }

            var chunks = connectionString.Split(';')
                         .Where(s => !string.IsNullOrWhiteSpace(s) && Regex.IsMatch(s, "^[^=]+=[^=]+$"))
                         .Select(s =>
            {
                var chunk = s.Split('=');

                return(new
                {
                    Key = chunk[0],
                    Value = chunk[1]
                });
            })
                         .ToList();

            var appId       = chunks.SingleOrDefault(c => string.Equals(c.Key, "AppId", StringComparison.InvariantCultureIgnoreCase));
            var appSecret   = chunks.SingleOrDefault(c => string.Equals(c.Key, "AppSecret", StringComparison.InvariantCultureIgnoreCase));
            var appPassword = chunks.SingleOrDefault(c => string.Equals(c.Key, "AppPassword", StringComparison.InvariantCultureIgnoreCase));

            if (appId == null ||
                appSecret == null ||
                appPassword == null)
            {
                SerilogLogger.Instance.Warning
                (
                    "Failed to add Configur because the connection string is invalid. AppId='{AppId}' AppSecret='{AppSecret}' AppPassword='******'",
                    appId,
                    appSecret,
                    appPassword
                );

                return(extended);
            }

            var configurOptions = new ConfigurOptions();

            options(configurOptions);

            var configurationSource = new ConfigurConfigurationSource
                                      (
                appId.Value,
                appSecret.Value,
                appPassword.Value,
                configurOptions,
                new HttpClient
                (
                    new LoggingHandler
                    (
                        new HttpClientHandler(),
                        new Logger <LoggingHandler>
                        (
                            new SerilogLoggerFactory
                            (
                                SerilogLogger.Instance
                            )
                        )
                    )
                )
            {
                Timeout = TimeSpan.FromSeconds(5),
            }
                                      );

            extended.Add(configurationSource);

            return(extended);
        }