2016-09-29 62 views
0

我有訪問集合DocumentDB的API的問題。如果我從我的開發環境(Visual Studio)運行API,它運行良好,並返回集合中的所有JSON文檔。等待時間約爲1分鐘。但是,當API部署在Azure中時,它不會返回任何內容。我還沒有在API中實現Application Insight。從Azure中部署的App API無法訪問Azure中的DocumentDB集合

的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,也沒有分區。

+0

埃德加,其中C#SDK的版本是你使用?你確定你連接到你的應用程序在相同的數據庫/集合天藍色(最好的方式來確認是打印出來的日誌)?你還可以檢查第一個CreateDocumentQuery是返回0結果還是第二個。如果某些文檔沒有dateCreated屬性,DateTime.Parse會拋出,所以確保不是這樣。我會在各個地方添加足夠的日誌以查看問題所在。你的代碼對我來說很好。 –

+0

既然你說相同的代碼在本地運行......你確認你的連接字符串是否正確(例如,你通過'AppSettings'正確設置/檢索數據庫和集合名稱)? –

回答

0

症狀表明應用程序無法連接到數據庫。這可能是由於應用程序無法讀取在Azure應用程序服務設置中定義的數據庫端點和密鑰。

string DatabaseId = ConfigurationManager.AppSettings["database"]; 
string CollectionId = ConfigurationManager.AppSettings["collectionExperimentos"]; 

DocumentClient client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"], 

你可以找到關於如何配置Azure的應用服務環境變量和連接字符串的更多信息(我假設它是一個Web應用程序)在這裏:https://azure.microsoft.com/en-us/documentation/articles/web-sites-configure/