【发布时间】:2016-09-29 22:33:58
【问题描述】:
我在使用 API 访问集合 DocumentDB 时遇到问题。如果我从我的开发环境 (Visual Studio) 运行 API,它运行良好并返回集合中的所有 JSON 文档。延迟时间约为 1 分钟。但是当 API 部署在 Azure 中时,它不会返回任何内容。我还没有在 API 中实现 Application Insight。
API 的 C# 代码是:
string DatabaseId = ConfigurationManager.AppSettings["database"];
string CollectionId = ConfigurationManager.AppSettings["collectionExperimentos"];
DocumentClient client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"],
new ConnectionPolicy
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp
});
var collectionLink = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
List<Experiment> experimentosList = new List<Experiment>();
experimentosList = client.CreateDocumentQuery<Experiment>(collectionLink).ToList();
experimentosList = experimentosList.OrderByDescending(experimentos => DateTime.Parse(experimentos.dateCreated)).ToList();
集合大小为 160MB,没有分区。
【问题讨论】:
-
Edgar,您使用的是哪个版本的 C# SDK?您确定要在 azure 中连接到应用程序中的同一数据库/集合(确认的最佳方法是将它们打印为日志)?您是否还可以检查第一个 CreateDocumentQuery 是返回 0 结果还是第二个结果。如果您的某些文档没有 dateCreated 属性,则 DateTime.Parse 将抛出,因此请确保不是这种情况。我会在各个地方添加足够的日志记录以查看问题所在。你的代码在我看来不错。
-
既然你说相同的代码在本地运行......你确认你的连接字符串是正确的(例如你通过
AppSettings正确设置/检索数据库和集合名称)?
标签: c# azure azure-cosmosdb azure-app-api