public static bool TryParse(string connectionString, out CosmosDBConnectionString parsedConnectionString)
        {
            if (String.IsNullOrWhiteSpace(connectionString))
            {
                parsedConnectionString = null;
                return(false);
            }

            try
            {
                return(ParseImpl(connectionString, out parsedConnectionString, err => { }));
            }
            catch (Exception)
            {
                parsedConnectionString = null;
                return(false);
            }
        }
        private static bool ParseImpl(string connectionString, out CosmosDBConnectionString parsedConnectionString, Action <string> error)
        {
            IDictionary <string, string> settings = ParseStringIntoSettings(connectionString, error);

            if (settings == null)
            {
                parsedConnectionString = null;
                return(false);
            }

            if (!RequireSettings.IsSubsetOf(settings.Keys))
            {
                parsedConnectionString = null;
                return(false);
            }

            parsedConnectionString = new CosmosDBConnectionString(
                new Uri(settings[AccountEndpointKey]), settings[AccountKeyKey]);
            return(true);
        }
示例#3
0
        private static void EnsureClient(ExecutionContext context)
        {
            if (cosmosDbClient != null)
            {
                return;
            }

            lock (staticLock)
            {
                if (cosmosDbClient != null)
                {
                    return;
                }

                IConfigurationRoot config = new ConfigurationBuilder()
                                            .SetBasePath(context.FunctionAppDirectory)
                                            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                                            .AddEnvironmentVariables()
                                            .Build();

                string connectionStringValue = config.GetConnectionString(ConnectionStringName);

                if (String.IsNullOrWhiteSpace(connectionStringValue))
                {
                    throw new InvalidOperationException(
                              Invariant($"Connection string '{ConnectionStringName}' has not been defined."));
                }

                var connectionString = CosmosDBConnectionString.Parse(connectionStringValue);

                cosmosDbClient = new DocumentClient(
                    connectionString.Endpoint,
                    connectionString.AuthKey,
                    CosmosDBConnectionString.DefaultPolicy);
            }
        }