部署到Azure后DocumentClient无法正常工作

时间:2018-10-22 09:29:36

标签: c# asp.net-core asp.net-core-2.0 azure-cosmosdb

我正在研究POC(在.Net Core中),以使用Azure DocumentClient将一些数据写入本地Cosmos DB仿真器。

private string _endpointUri = String.Empty;
private string _primaryKey = String.Empty;
private DocumentClient _client;
private string _databaseName = String.Empty;
private string _collectionName = String.Empty;

public CosmosDBStorage(IConfiguration configuration)
{
    _endpointUri = configuration["CosmosDBEndpointUri"];
    _primaryKey = configuration["CosmosDBPrimaryKey"];
    _client = new DocumentClient(new Uri(_endpointUri), _primaryKey);
    _databaseName = configuration["CosmosDBName"];
    _collectionName = configuration["CosmosDBCollectionName"];
}

public async Task WriteAsync(dynamic data)
{
    try
    {
        await this._client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), data);
    }
    catch
    {
        throw;
    }
}

它在我的本地环境中运行良好,但是,在将应用程序部署到Azure之后,我尝试使用相同的本地Cosmos DB模拟器进行测试。它失败并显示以下错误。

异常消息: 发送请求时出错

异常堆栈跟踪:

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Client.DocumentClient.HttpRequestMessageHandler.<SendAsync>d__3.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Client.GatewayServiceConfigurationReader.<GetDatabaseAccountAsync>d__43.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Routing.GlobalEndpointManager.<GetDatabaseAccountFromAnyLocationsAsync>d__15.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Client.GatewayServiceConfigurationReader.<InitializeReaderAsync>d__45.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Client.DocumentClient.<InitializeGatewayConfigurationReader>d__319.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Client.DocumentClient.<GetInitializationTask>d__85.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Client.DocumentClient.<EnsureValidClientAsync>d__135.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Client.DocumentClient.<GetCollectionCacheAsync>d__70.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.Client.DocumentClient.<CreateDocumentInlineAsync>d__143.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1T0
at BotFrameworkDev.Storage.CosmosDBStorage.<WriteAsync>d__7.MoveNext() in C:\GIS\ChatBot\BotFrameworkDev\BotFrameworkDev\Storage\CosmosDBStorage.cs:line 42
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at BotFrameworkDev.EchoBot.<OnTurn>d__8.MoveNext() in C:\GIS\ChatBot\BotFrameworkDev\BotFrameworkDev\EchoBot.cs:line 99

任何想法可能是什么问题?

更新

添加了内部异常详细信息。

异常消息:无法建立与服务器的连接

异常堆栈跟踪:

at BotFrameworkDev.Storage.CosmosDBStorage.<WriteAsync>d__7.MoveNext() in C:\GIS\ChatBot\BotFrameworkDev\BotFrameworkDev\Storage\CosmosDBStorage.cs:line 42
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at BotFrameworkDev.EchoBot.<OnTurn>d__8.MoveNext() in C:\GIS\ChatBot\BotFrameworkDev\BotFrameworkDev\EchoBot.cs:line 99

1 个答案:

答案 0 :(得分:1)

您不能使用本地模拟器在Azure中进行测试。 Azure无法找到您的localhost,因为它不在其网络范围之内。

它将在自己的网络中搜索并失败。您将需要指向实际的CosmosDB实例,或者以某种方式将模拟器从服务器或PC暴露给Azure应用服务。

请记住,CosmosDB Emulator仅应用于本地开发,而不能用于其他用途。它应该是模仿行为,而不是真实的东西。

推荐的方法是创建一个CosmosDB帐户并使用该连接字符串。