コード例 #1
0
 /// <summary>
 /// Initialize a CosmosClient by providing the accountName and key.
 /// If database or container do not exist under the account, the <paramref name="createOptions"/> must be provided and include required parameters to create the database and container.
 /// If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.
 /// If database and container exist under the account, <paramref name="createOptions"/> will be ignored if passed in.
 /// </summary>
 /// <param name="accountEndpoint">Endpoint of the Cosmos account to connect to. (i.e https://youraccount.documents.azure.com:443/) </param>
 /// <param name="key">Account Key from the Key blade in the portal</param>
 /// <param name="databaseId">Id of the Database to connect to.</param>
 /// <param name="containerId">Id of the Container to connect to.</param>
 /// <param name="createOptions">Speficies the options for creating a new database and contianer if need be (throughput, partitionKey, indexing strategy, TTL etc..)</param>
 /// <exception cref="Exception">If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.</exception>
 /// <returns>Reference to a Sql CosmosClient</returns>
 public static Task <ICosmosClientSql> GetByAccountEndpoint(string accountEndpoint, string key, string databaseId, string containerId, CreateOptions createOptions = null)
 {
     return(GetCosmosDbClientInternal(new CosmosClient(accountEndpoint, key, createOptions?.ClientOptions ?? CreateOptions.DefaultClientOptions), databaseId, containerId, createOptions));
 }
コード例 #2
0
 /// <summary>
 /// Initialize a CosmosClient by providing the accountName and key.
 /// If database or container do not exist under the account, the <paramref name="createOptions"/> must be provided and include required parameters to create the database and container.
 /// If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.
 /// If database and container exist under the account, <paramref name="createOptions"/> will be ignored if passed in.
 /// </summary>
 /// <param name="connectionString">Connection String to a CosmosDB. (i.e. AccountEndpoint=***;AccountKey=***;)</param>
 /// <param name="databaseId">Id of the Database to connect to.</param>
 /// <param name="containerId">Id of the Container to connect to.</param>
 /// <param name="createOptions">Speficies the options for creating a new database and contianer if need be (throughput, partitionKey, indexing strategy, TTL etc..)</param>
 /// <exception cref="Exception">If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.</exception>
 /// <returns>Reference to a Sql CosmosClient</returns>
 public static Task <ICosmosClientSql> GetByConnectionString(string connectionString, string databaseId, string containerId, CreateOptions createOptions = null)
 {
     return(GetCosmosDbClientInternal(new CosmosClient(connectionString, createOptions?.ClientOptions ?? CreateOptions.DefaultClientOptions), databaseId, containerId, createOptions));
 }
コード例 #3
0
        /// <summary>
        /// Initialize a CosmosClient by providing the accountName and key.
        /// If database or container do not exist under the account, the <paramref name="createOptions"/> must be provided and include required parameters to create the database and container.
        /// If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.
        /// If database and container exist under the account, <paramref name="createOptions"/> will be ignored if passed in.
        /// </summary>
        /// <param name="accountName">Name of the Cosmos account to connect to. (i.e [yourAccount] from -> https://yourAccount.documents.azure.com:443/)</param>
        /// <param name="key">Account Key from the Key blade in the portal</param>
        /// <param name="databaseId">Id of the Database to connect to.</param>
        /// <param name="containerId">Id of the Container to connect to.</param>
        /// <param name="createOptions">Speficies the options for creating a new database and contianer if need be (throughput, partitionKey, indexing strategy, TTL etc..)</param>
        /// <exception cref="Exception">If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.</exception>
        /// <returns>Reference to a Sql CosmosClient</returns>
        public static Task <ICosmosClientSql> GetByAccountName(string accountName, string key, string databaseId, string containerId, CreateOptions createOptions = null)
        {
            var accountEndpoint = string.Format(CosmosClientSql.SqlAccountEndpointFormat, accountName);

            return(GetByAccountEndpoint(accountEndpoint, key, databaseId, containerId, createOptions));
        }
コード例 #4
0
        private static async Task <ICosmosClientSql> GetCosmosDbClientInternal(CosmosClient client, string databaseId, string containerId, CreateOptions createOptions = null)
        {
            Database  database  = null;
            Container container = null;

            if (createOptions != null)
            {
                database = await client.CreateDatabaseIfNotExistsAsync(databaseId, createOptions.DatabaseThrouhput);

                container = await database.CreateContainerIfNotExistsAsync(createOptions.Container, createOptions.ContainerThroughput);
            }
            else
            {
                database = client.GetDatabase(databaseId);
                var ensureDbExists = await database.ReadAsync();

                if (ensureDbExists.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    throw new Exception($"Database '{databaseId}' not found. Use forceCreate:true if you want the database to be created for you.");
                }

                container = database.GetContainer(containerId);
                var ensureExists = await container.ReadContainerAsync();

                if (ensureExists.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    throw new Exception($"Container '{containerId}' not found. Use forceCreate:true if you want a collection to be created for you.");
                }
            }


            var res = new CosmosClientSql
            {
                Client    = client,
                Database  = database,
                Container = container
            };

            var r = await container.ReadContainerAsync();

            res._partitionKeyPropertyName = r.Resource.PartitionKeyPath.Trim('/');
            res.CosmosSerializer          = new CosmosEntitySerializer(res._partitionKeyPropertyName);
            return(res);
        }
コード例 #5
0
        /// <summary>
        /// Initialize a CosmosClient by providing the accountName and key.
        /// If database or container do not exist under the account, the <paramref name="createOptions"/> must be provided and include required parameters to create the database and container.
        /// If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.
        /// If database and container exist under the account, <paramref name="createOptions"/> will be ignored if passed in.
        /// </summary>
        /// <param name="accountEndpoint">Endpoint of the Cosmos account to connect to. (i.e https://youraccount.documents.azure.com:443/) </param>
        /// <param name="key">Account Key from the Key blade in the portal</param>
        /// <param name="databaseId">Id of the Database to connect to.</param>
        /// <param name="containerId">Id of the Container to connect to.</param>
        /// <param name="createOptions">Speficies the options for creating a new database and contianer if need be (throughput, partitionKey, indexing strategy, TTL etc..)</param>
        /// <exception cref="Exception">If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.</exception>
        /// <returns>Reference to a Sql CosmosClient</returns>
        public static Task <ICosmosClientSql> GetByAccountEndpoint(string accountEndpoint, string key, string databaseId, string containerId, CreateOptions createOptions = null)
        {
            var cco = new CosmosClientOptions()
            {
                ConnectionMode = ConnectionMode.Direct,
                RequestTimeout = new TimeSpan(1, 0, 0),
            };

            return(GetCosmosDbClientInternal(new CosmosClient(accountEndpoint, key, cco), databaseId, containerId, createOptions));
        }
コード例 #6
0
        /// <summary>
        /// Initialize a SQL backed Cosmos Graph Client.
        /// If database or container do not exist under the account, the <paramref name="createOptions"/> must be provided and include required parameters to create the database and container.
        /// If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.
        /// If database and container exist under the account, <paramref name="createOptions"/> will be ignored if passed in.
        /// </summary>
        /// <param name="accountName">Name of the Cosmos account to connect to. (i.e [yourAccount] from -> https://yourAccount.documents.azure.com:443/)</param>
        /// <param name="key">Account Key from the Key blade in the portal</param>
        /// <param name="databaseId">Id of the Database to connect to.</param>
        /// <param name="containerId">Id of the Container to connect to.</param>
        /// <param name="createOptions">Speficies the options for creating a new database and contianer if need be (throughput, partitionKey, indexing strategy, TTL etc..)</param>
        /// <exception cref="Exception">If database or container do not exist under the account, and a <paramref name="createOptions"/> is not provided, the method will throw an exception.</exception>
        /// <returns>Reference to a Graph CosmosClient</returns>
        public static async Task <ICosmosClientGraph> GetClientWithSql(string accountName, string key, string databaseId, string containerId, CreateOptions createOptions = null)
        {
            var sqlClient = (CosmosClientSql)(await CosmosDB.Net.CosmosClientSql.GetByAccountName(accountName, key, databaseId, containerId, createOptions));

            var gremlinEndpoint = string.Format(CosmosClientGraph.GraphEndpointFormat, accountName);
            var server          = new GremlinServer(gremlinEndpoint, 443, username: "******" + databaseId + "/colls/" + containerId, enableSsl: true, password: key);

            return(new CosmosClientGraph
            {
                GremlinServer = server,
                CosmosSqlClient = sqlClient,
                CosmosSerializer = sqlClient.CosmosSerializer,
            });
        }