【问题标题】:Unable to connect to Cosmos DB while creating Azure function through Visual Studio通过 Visual Studio 创建 Azure 函数时无法连接到 Cosmos DB
【发布时间】:2020-07-06 09:38:11
【问题描述】:

我是 Azure Functions 和 Cosmos DB 的新手。我正在尝试在我的本地计算机上设置一个 Azure 函数,该函数会在通过 Visual Studio 更新 Cosmos DB 时触发。

最初,我收到了错误 Unable to resolve app setting for property CosmosDBTriggerAttribute.ConnectionStringSetting,但在关注此 solution 后,我能够解决该错误,但现在得到:

The listener for function 'Function1' was unable to start. System.Net.Http: No such host is known. System.Private.CoreLib: No such host is known.

这是我的local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "connectionString": "AccountEndpoint=https://<collectionName>.documents.azure.com:443/;AccountKey=<key>"
  }
}

如果有用的话,这是我的运行方法:

public static void Run([CosmosDBTrigger(
            databaseName: "DB-Name",
            collectionName: "CollectionName",
            ConnectionStringSetting = "String I used while setting up the project in VS",
            LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
        {
            if (input != null && input.Count > 0)
            {
                log.LogInformation("Documents modified " + input.Count);
                log.LogInformation("First document Id " + input[0].Id);
            }
        }

有人可以帮忙吗?我的另一个问题是 Account Key 在这种情况下是什么?我目前在我的 Cosmos DB 帐户中使用该应用名称。

【问题讨论】:

    标签: azure azure-functions azure-cosmosdb


    【解决方案1】:

    当您点击您的cosmosDB resource 时,您可以从Settings -&gt; ConnectionString -&gt; Primary Password 找到AccountKey

    而且你的 connectionString 应该如下所示,

    {
      "IsEncrypted": false,
      "Values": {
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "CosmosDB": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
      }
    }
    

    【讨论】:

    • 你需要确保数据库名称和集合是正确的
    • 感谢您的回答。我更改了相关端点和帐户密钥,但现在我面临一个新错误:Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Cannot create Collection Information for &lt;collection&gt; in database &lt;Database&gt; with lease leases in database &lt;database&gt; : , Request URI: //, RequestStats: , SDK: Windows/10.0.18363 documentdb-netcore-sdk/2.3.0. Microsoft.Azure.DocumentDB.Core: , Request URI: //, RequestStats: , SDK: Windows/10.0.18363 documentdb-netcore-sdk/2.3.0.
    • 是的,数据库名称和集合都是正确的,并且与 Azure 门户一致。
    • 我检查了您共享的链接。我交叉检查了,但我连接到了正确的帐户,并且 cosmos DB 帐户中没有防火墙设置。
    猜你喜欢
    • 2018-06-14
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多