【发布时间】:2019-01-16 13:50:00
【问题描述】:
我有一个使用 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,我认为它不应该抛出这个错误。
有人可以帮忙吗?
【问题讨论】:
-
2 个问题。您是否在使用模拟器和真实服务时遇到相同的错误?您是否也可以通过 HTTPS 设置网关,看看您是否收到该错误?
-
嘿,我的API在本地是在HTTPS上运行的,你是说它必须是真正的SSL吗?至于模拟器,我不知道有一个。有链接吗?
-
我说的是 CosmosDB。我真正要求的只是将
ConnectionMode.Direct更改为ConnectionMode.Gateway和Protocol.Tcp更改为Protocol.Https并告诉我这是否有效。不是解决方案,但会缩小问题范围。当然,这里是模拟器文档页面:docs.microsoft.com/en-us/azure/cosmos-db/local-emulator -
嗯,如果我把它改成你说的,我会得到一个新的错误:ResourceType Document is unexpected
标签: c# azure azure-cosmosdb