代码之家  ›  专栏  ›  技术社区  ›  Souvik Ghosh

部署到azure后,documentclient不工作

  •  1
  • Souvik Ghosh  · 技术社区  · 7 年前

    我正在开发一个poc(在.net内核中),使用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 回复  |  直到 7 年前
        1
  •  1
  •   Nick Chapsas    7 年前

    不能使用本地模拟器在azure中进行测试。azure找不到你的 localhost 因为它超出了网络的界限。

    它将在自己的网络中搜索并失败。你需要指向一个实际的cosmosdb实例,或者以某种方式将你的模拟器从服务器或你的pc公开到azure应用服务。

    请记住,cosmosdb仿真器只用于本地开发,不用于其他用途。它被认为是模仿行为而不是真实的东西。

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

    推荐文章