public void TryGetConnectionString_NotFound() { string key = "ConnectionStrings_Accounts-MongoDb_nonexistant"; IConnectionStringProvider provider = new EnvironmentVariableConnectionStringProvider(key); bool result = provider.TryGetConnectionString(out string outConnection); Assert.False(result, "The connection string should not be found."); Assert.Null(outConnection); }
public void TryGetConnectionString() { string key = "ConnectionStrings_Accounts-MongoDb"; string connString = "mongodb://some-mongo-instance:27017/some-db"; Environment.SetEnvironmentVariable(key, connString); IConnectionStringProvider provider = new EnvironmentVariableConnectionStringProvider(key); bool result = provider.TryGetConnectionString(out string outConnection); Assert.True(result, "Environment variable not found"); Assert.Equal(outConnection, connString); }
private static IConnectionStringProvider ParseConnectionStringProvider( JsonNode node, IFilePathMapper pathMapper, Func <IJsonNode, IConnectionStringProvider> connectionStringProviderOverride) { if (node.AsObject == null) { return(new StaticConnectionStringProvider(node.Value)); } IConnectionStringProvider provider = connectionStringProviderOverride?.Invoke(node); if (provider != null) { return(provider); } JsonClass objClass = node.AsObject; if (!Enum.TryParse(objClass["locator"]?.Value, out SupportedConnectionStringLocators locator)) { throw new NAMEException($"The locator {objClass["locator"]?.Value} is not supported.", NAMEStatusLevel.Warn); } string key; switch (locator) { #if NET45 case SupportedConnectionStringLocators.ConnectionStrings: key = objClass["key"]?.Value; provider = new ConnectionStringsConnectionStringProvider(key); break; case SupportedConnectionStringLocators.AppSettings: key = objClass["key"]?.Value; provider = new AppSettingsConnectionStringProvider(key); break; case SupportedConnectionStringLocators.VSSettingsFile: key = objClass["key"]?.Value; string section = objClass["section"]?.Value; if (string.IsNullOrEmpty(section)) { throw new ArgumentNullException("section", "The section must be specified."); } provider = new VisualStudioSetingsFileConnectionStringProvider(section, key); break; #endif case SupportedConnectionStringLocators.JSONPath: { key = objClass["expression"]?.Value; string file = objClass["file"]?.Value; if (string.IsNullOrEmpty(file)) { throw new ArgumentNullException("file", "The file must be specified."); } provider = new JsonPathConnectionStringProvider(pathMapper.MapPath(file), key); } break; case SupportedConnectionStringLocators.XPath: { key = objClass["expression"]?.Value; string file = objClass["file"]?.Value; if (string.IsNullOrEmpty(file)) { throw new ArgumentNullException("file", "The file must be specified."); } provider = new XpathConnectionStringProvider(pathMapper.MapPath(file), key); break; } case SupportedConnectionStringLocators.EnvironmentVariable: { key = objClass["key"]?.Value; provider = new EnvironmentVariableConnectionStringProvider(key); } break; default: throw new NAMEException($"The locator {locator.ToString()} is not supported.", NAMEStatusLevel.Warn); } if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException($"The connection string key/expression must be specified."); } return(provider); }