问题
I am trying to create a collection if it does not exist.
First, I create my DocumentClient by:
public DocumentClient CreateClient(ConnectionPolicy connectionPolicy = null)
{
return new DocumentClient(new Uri(this.AccountEndpoint), this.AccountKey, connectionPolicy);
}
Now that I have my document client (let's call it documentDbClient
), I attempt to use CreateDocumentCollectionIfNotExistsAsync
:
var documentDbClient = dbConnection.CreateClient();
await documentDbClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("mydatabase"),
new DocumentCollection { Id = "testcollection" });
Now, I would expect this to create a collection named testcollection
under my database named mydatabase
. However, nothing happens. No collection is created, no exceptions are thrown.
I thought perhaps it was just slow, and so I waited 30 minutes and went back to the portal to see if my collection had been made...
It hadn't.
What is wrong?
UPDATE
Okay, so I have discovered that if I change the name of the collection that I am attempting to create, it works. I have previously created and deleted the collection many times and now it appears that Azure is failing to create it 90% of the time.
回答1:
So, you're likely running into a throttling situation when repeatedly creating and destroying collections. Here's an excerpt from the Table API section of the FAQ (which applies equally to document collections):
The rapid rate of creation of tables is detected and throttled. We recommend that you look at the rate of creation of tables and lower it to less than 5 per minute.
The current throttle rate is 5 creates / minute. So, for a unit-testing scenario, you'd need to work around this (e.g. keeping a collection, but just emptying its contents after each test run).
Note: I notified the product team, that the throttle mention is only in the Table API section, and to update the FAQ accordingly.
来源:https://stackoverflow.com/questions/44991300/createdocumentcollectionifnotexistsasync-does-not-always-work