首页 > 解决方案 > 无法从 VS Code Azure 函数连接到 CosmosDB

问题描述

我从 C# HTTP 模板创建了 Azure Function (2.0v)。然后我根据CosmosDB 文档将输出绑定添加到 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 属性的情况下运行良好。

标签: azureazure-functionsazure-cosmosdb

解决方案


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


推荐阅读