コード例 #1
0
        internal static async Task <DocumentCollection> CreateNonPartitionedContainer(
            Cosmos.Database database,
            DocumentCollection documentCollection)
        {
            (string endpoint, string authKey)accountInfo = TestCommon.GetAccountInfo();

            //Creating non partition Container, rest api used instead of .NET SDK api as it is not supported anymore.
            HttpClient client      = new System.Net.Http.HttpClient();
            Uri        baseUri     = new Uri(accountInfo.endpoint);
            string     dateTimeUtc = NonPartitionedContainerHelper.GetUtcDateTime();

            string verb         = "POST";
            string resourceType = "colls";
            string resourceId   = string.Format("dbs/{0}", database.Id);
            string resourceLink = string.Format("dbs/{0}/colls", database.Id);

            client.DefaultRequestHeaders.Add("x-ms-date", dateTimeUtc);
            client.DefaultRequestHeaders.Add("x-ms-version", NonPartitionedContainerHelper.PreNonPartitionedMigrationApiVersion);

            string authHeader = NonPartitionedContainerHelper.GenerateMasterKeyAuthorizationSignature(
                verb,
                resourceId,
                resourceType,
                accountInfo.authKey,
                "master",
                "1.0",
                dateTimeUtc);

            client.DefaultRequestHeaders.Add("authorization", authHeader);
            string        containerDefinition = documentCollection.ToString();
            StringContent containerContent    = new StringContent(containerDefinition);
            Uri           requestUri          = new Uri(baseUri, resourceLink);

            DocumentCollection responseCollection = null;

            using (HttpResponseMessage response = await client.PostAsync(requestUri.ToString(), containerContent))
            {
                response.EnsureSuccessStatusCode();
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, response.ToString());
                responseCollection = await response.Content.ToResourceAsync <DocumentCollection>();
            }

            return(responseCollection);
        }
コード例 #2
0
        public async Task <ResourceResponse <DocumentCollection> > CreateCollectionAsync(string databaseId, DocumentCollection collection,
                                                                                         RequestOptions requestOptions = null)
        {
            var databaseUri = UriFactory.CreateDatabaseUri(databaseId);

            return(await this.InvokeCosmosOperationAsync(() => DocumentClient.CreateDocumentCollectionAsync(databaseUri, collection, requestOptions), collection.ToString())
                   .ExecuteCosmosCommand());
        }