c# - 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时,我认为它不应该抛出这个错误。
有人可以帮忙吗?
解决方案
原来我的保存方法创建了错误的链接。我正在使用:
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);
}
}
推荐阅读
- tensorflow - keras.layers.Conv2D 的默认内核大小、零填充和步幅是多少?
- javascript - 使用 jquery 中另一个复选框的名称设置文本的值
- regex - 如何从shell脚本中的文件名中提取字符串?
- apache - 如何强制增加 Apache 中视频的范围字节响应的大小?
- .net - 使用 SQL Service Broker 从 SQL Server 调用 API
- r - 我在 R 中的函数没有绘制绘图
- c# - 迭代地从字符串的反身属性中检索值时出现 TargetParameterCountException
- javascript - Babel plugin-proposal-decorators 没有按预期工作
- python - Django div重新加载 - POST后的ajax GET没有获取最新的数据库模型实例
- php - 递归 AJAX 调用被取消