【问题标题】:Cannot connect to CosmosDB from VS Code Azure Function无法从 VS Code Azure 函数连接到 CosmosDB
【发布时间】:2018-06-04 12:55:02
【问题描述】:

我从 C# HTTP 模板创建了 Azure Function (2.0v)。然后我添加了基于CosmosDB docs的输出绑定到CosmosDB:

public static class AddEvent

{
    [FunctionName("AddEvent")]
    public static void Run(

        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
            HttpRequest req, 
        [CosmosDB("SomeDatabase", "SomeCollection", Id = "id", 
        ConnectionStringSetting = "myCosmosDB", CreateIfNotExists = true)] 
            out dynamic document)
    {
        document = new { Text = "something", id = Guid.NewGuid() };
    }
}

我使用的包(csproj 文件):

<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.0-beta7" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.11" />

这是我的 local.settings.json。我基于 CosmosDB 模拟器中的值:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "AzureWebJobsDashboard": "",
        "myCosmosDB": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
    }
}

不幸的是,当我点击 HTTP 触发器时,我得到:

System.Private.CoreLib: Exception while executing function: AddEvent.
Microsoft.Azure.WebJobs.Host: Exception binding parameter 'document'.
Microsoft.Azure.DocumentDB.Core: The type initializer for 'Microsoft.Azure.Documents.UserAgentContainer' threw an exception. 
Object reference not set to an instance of an object.

这个异常是什么意思?我找不到任何相关信息,它完全停止了我的本地工作。函数在没有 CosmosDB 属性的情况下运行良好。

【问题讨论】:

  • 看起来类似于this issue。你有哪个确切的版本?
  • 我有版本号。 28. 但是降级到 24 并没有帮助 - 它导致 Microsoft.Azure.WebJobs.Extensions.CosmosDB: Method not found: Void Microsoft.Azure.WebJobs.Host.Config.FluentBindingRule1.AddValidator(System.Action2&lt;!0,System.Type&gt;)'. 错误。不幸的是,这两个12 决议也没有帮助。
  • 问题已在 29 cli 中解决。您可以接受一个答案来结束此问题。

标签: azure azure-functions azure-cosmosdb


【解决方案1】:

这是最新版本 (2.0.11776) 主机的回归。此问题已得到解决,目前正在发布。

【讨论】:

    【解决方案2】:

    这应该是新 cli 中的重大变化。尝试将 cli 降级为2.0.1-beta.25,它适用于我。顺便说一句,我建议您将Microsoft.NET.Sdk.Functions 更新为1.0.13 以避免可能的异常。

    beta.24 似乎已经过时,并且也会导致我这边出现Method not found 错误。而beta.26 导致与beta.28 相同的错误。

    但是,如果我直接使用 VS(使用 beta.26 no-runtime version)调试项目或将其发布到 Azure,则可以正常工作。已开通an issue on github,有兴趣可以追踪。

    更新

    已解决在2.0.1-beta.29,运行时2.0.11857.0

    【讨论】:

      【解决方案3】:

      您引用的是哪个版本的 DocumentDB 包?

      建议是:

      将任何 DocumentDB nuget 降级为 1.13.2.

      完全删除 DocumentDB 引用,而是直接引用 Microsoft.Azure.WebJobs.Extensions.DocumentDB,它将为您引用正确的版本。

      【讨论】:

      • 我在我的csproj中直接使用&lt;PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.0-beta7" /&gt;。但这无济于事。
      • OP 正在使用 Functions V2,此时 V2 的 SDK 版本高于 V1。
      猜你喜欢
      • 1970-01-01
      • 2018-12-11
      • 2018-05-07
      • 1970-01-01
      • 2019-06-29
      • 2019-11-27
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多