代码之家  ›  专栏  ›  技术社区  ›  Zaphod

Azure函数,输出到cosmos db

  •  0
  • Zaphod  · 技术社区  · 6 年前

    我正在尝试创建一个函数,它将接受一些输入并将其存储在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 回复  |  直到 6 年前
        1
  •  1
  •   brettsam    6 年前

    尝试删除 Id 属性中的参数。当您指定 身份证件 ,函数将尝试查询该文档。在您的情况下,您不希望这样做,因为您正在尝试进行输出绑定。

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

        2
  •  1
  •   Zaphod    6 年前

    我不知道到底是什么解决了这个问题。但我按照@Vladislav的建议做了,并清洗了溶液。我还重新安装了所有软件包,并重新检查了所有连接字符串。出于某种原因,它现在正在发挥作用。 谢谢你的建议!

        3
  •  0
  •   Jay Gong    6 年前

    请参考以下代码:

    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 (不应通过编译)。

    希望对你有帮助。