我有一个使用 azure cosmosdb 的类(class)。我的类(class)如下所示:
public class DocumentService : IDocumentService
{
private readonly DocumentClient _client;
private readonly string _collectionName;
private readonly string _databaseName;
public DocumentService(IDocumentDbConfig settings)
{
var connectionPolicy = new ConnectionPolicy
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp,
RequestTimeout = new TimeSpan(1, 0, 0),
MaxConnectionLimit = 1000,
RetryOptions = new RetryOptions
{
MaxRetryAttemptsOnThrottledRequests = 10,
MaxRetryWaitTimeInSeconds = 60
}
};
_databaseName = settings.DocumentDbDatabaseName;
_collectionName = settings.DocumentDbProductsCollectionName;
_client = new DocumentClient(new Uri(settings.DocumentDbEnpointUrl), settings.DocumentDbPrimaryKey, connectionPolicy);
}
public IList<JObject> List(string query = "SELECT * FROM c") => _client.CreateDocumentQuery<JObject>(UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), query, new FeedOptions {EnableCrossPartitionQuery = true}).AsEnumerable().ToList();
public async Task SaveAsync(IEnumerable<JObject> models)
{
foreach (var document in models) {
var documentLink = UriFactory.CreateDocumentUri(_databaseName, _collectionName, document["id"].ToString());
await _client.CreateDocumentAsync(documentLink, document);
}
}
public async Task DeleteAsync(string documentName, string partitionKey)
{
var requestOptions = new RequestOptions { PartitionKey = new PartitionKey(partitionKey) };
var documentUri = UriFactory.CreateDocumentUri(_databaseName, _collectionName, documentName);
await _client.DeleteDocumentAsync(documentUri, requestOptions);
}
public async Task DeleteMultipleAsync(string partitionKey)
{
var requestOptions = new RequestOptions { PartitionKey = new PartitionKey(partitionKey) };
var query = $"SELECT * FROM c WHERE c.categoryId = '{partitionKey}'";
var response = _client.CreateDocumentQuery<JObject>(UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), query, new FeedOptions { EnableCrossPartitionQuery = true }).AsDocumentQuery();
while (response.HasMoreResults)
foreach (Document document in await response.ExecuteNextAsync())
await _client.DeleteDocumentAsync(document.SelfLink, requestOptions);
}
}
当我调用 SaveAsync 方法时,当它到达 await _client.CreateDocumentAsync(documentLink, document)
时,我收到错误。
错误是:
The MAC signature found in the HTTP request is not the same as the computed signature
由于我正在使用 Microsoft.Azure.DocumentDB,我认为它不应该抛出此错误。
有人可以帮忙吗?
最佳答案
事实证明我的保存方法创建了错误的链接。我正在使用:
var documentLink = UriFactory.CreateDocumentUri(_databaseName, _collectionName, document["id"].ToString());
当它应该是:
var collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName);
所以整个方法应该如下所示:
public async Task SaveAsync(IEnumerable<JObject> models)
{
foreach (var document in models)
{
var collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName);
await _client.CreateDocumentAsync(collectionLink, document);
}
}
关于c# - Azure.DocumentDb 在 HTTP 请求中找到的 MAC 签名与计算的签名不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54218513/