c# - Azure.DocumentDb 在 HTTP 请求中找到的 MAC 签名与计算的签名不同

标签 c# azure azure-cosmosdb

我有一个使用 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/

相关文章:

asp.net - Azure 配置设置比 Web.Config 更安全?

azure - Windows 服务到 Azure WorkerRoles

azure-cosmosdb - 如何并行查询不同类型的文档?

azure-cosmosdb - 简单的 DocumentDb 存储过程

c# - 如何使用 C# 代码挂载 VHD,而不调用 diskpart 脚本?

c# - 报表查看器中子报表的问题

c# - 获取产品 ID 和供应商 ID

c# - 一个程序在我的计算机上运行良好,但是当我将它发送到另一个使用荷兰 Windows 的计算机上时,它的 DateTimePicker 出现问题

azure - 为什么我无法在 azure devops yaml 模板中的 if 条件中传递运行时变量

azure - 将数据从 Cosmos 数据库帐户导出到另一个帐户的最佳方法