首页 > 解决方案 > Azure.DocumentDb HTTP 请求中找到的 MAC 签名与计算的签名不同

问题描述

我有一个使用 azure cosmosdb 的课程。我的课看起来像这样:

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).

错误是:

在 HTTP 请求中找到的 MAC 签名与计算的签名不同

当我使用Microsoft.Azure.DocumentDB时,我认为它不应该抛出这个错误。

有人可以帮忙吗?

标签: c#azureazure-cosmosdb

解决方案


原来我的保存方法创建了错误的链接。我正在使用:

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);
    }
}

推荐阅读