Azure.DocumentDb在HTTP请求中找到的MAC签名与计算出的签名

时间:2019-01-16 13:50:00

标签: c# azure azure-cosmosdb

我有一个使用Azureure 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 时,我认为它不会引发此错误。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

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

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