首页 文章

Azure函数,输出到cosmos db

提问于
浏览
0

我正在尝试创建一个需要输入并将其存储在cosmos db中的函数 . 我写过这个函数:

using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

    [FunctionName("DocumentUpdates")]
        public static void Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req,
            [CosmosDB("Fagkveld", "ViktigeData", CreateIfNotExists = true, Id = "Id", ConnectionStringSetting = "CosmosDbConn")]out dynamic document,
            TraceWriter log)
        {
            document = new {Id="Identifier1", Title = "Some Title"};
}

但是当我运行这个时,我收到以下错误:

[24.04.2018 07:05:15] Executed 'DocumentUpdates' (Failed, Id=ce0deab0-5ba3-4bb0-9399-96661c52ecb8)
[24.04.2018 07:05:15] System.Private.CoreLib: Exception while executing function: DocumentUpdates. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'document'. Microsoft.Azure.DocumentDB.Core: Value cannot be null.
[24.04.2018 07:05:15] Parameter name: serviceEndpoint.

有人能告诉我我做错了什么吗?我找不到任何serviceEndpoint参数?

3 回答

  • 1

    尝试从属性中删除 Id 参数 . 指定 Id 时,该函数将尝试查询该文档 . 在您的情况下,您不要尝试进行输出绑定 .

    [CosmosDB("Fagkveld", "ViktigeData", CreateIfNotExists = true, ConnectionStringSetting = "CosmosDbConn")]out dynamic document

  • 1

    我不确定到底是什么解决了这个问题 . 但我做了@Vladislav建议并清理了解决方案 . 我还重新安装了所有包,并重新检查了所有连接字符串 . 由于某种原因,它现在正在运作 . 感谢所有的建议!

  • 0

    请参考以下代码:

    using System.Net.Http;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    
    namespace JayGongCosmosDB
    {
        public static class Function2
        {
            [FunctionName("Function1")]
            public static void Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,
                 [DocumentDB("db", "coll", Id = "id", ConnectionStringSetting = "myCosmosDB")] out dynamic document)
            {
                document = new { Id = "Identifier1", Title = "Some Title" };
            }
        }
    }
    

    基于cosmos db output c# example,您需要使用 DocumentDB 而不是 CosmosDB (不应该通过编译) .

    希望它能帮到你 .

相关问题