F#Azure Function的Cosmos DB绑定无法绑定

霍华德·安达·埃斯坦森

我正在尝试在F#中创建一个具有HTTP触发器并从Cosmos DB检索数据的Azure函数。文档仅提供CosmosDB绑定的C#示例,并且翻译代码会导致错误Cannot bind parameter 'toDoItems' to type IEnumerable`1

这个C#示例能够记录Cosmos中各项的ID:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace Customer
{
    public class ToDoItem
    {
        public string Id { get; set; }
        public string PartitionKey { get; set; }
        public string Description { get; set; }
    }

    public static class DocByIdFromQueryString
    {
        [FunctionName("Triggered")]
        public static IActionResult Run(
            [HttpTrigger(
                AuthorizationLevel.Anonymous,
                "get",
                Route = null
            )] HttpRequest req,
            [CosmosDB(
                databaseName: "customer-db",
                collectionName: "todo-collection",
                ConnectionStringSetting = "CosmosDBConnection",
                SqlQuery = "select * from c"
            )] IEnumerable<ToDoItem> toDoItems, ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            foreach (ToDoItem toDoItem in toDoItems)
            {
                log.LogInformation(toDoItem.Id);
            }
            return new OkResult();
        }
    }
}

这是来自有效C#代码的F#转换。这两个示例具有相同的依赖关系版本,并使用相同的数据库(和连接字符串)。

namespace Customer

open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Mvc
open Microsoft.Azure.WebJobs
open Microsoft.Azure.WebJobs.Extensions.Http
open Microsoft.Extensions.Logging


module CosmosBindings =
    type ToDoItem = {
        Id : string
        PartitionKey : string
        Description : string
    }

    [<FunctionName("HttpTrigger")>]
    let Run([<HttpTrigger(
                AuthorizationLevel.Anonymous,
                "get",
                Route = null
            )>] req: HttpRequest,
            [<CosmosDB(
                databaseName = "customer-db",
                collectionName = "todo-collection",
                ConnectionStringSetting = "CosmosDBConnection",
                SqlQuery = "select * from c"
            )>] toDoItems: ToDoItem seq) (log: ILogger) =
                log.LogInformation "F# HTTP trigger function processed a request."

                toDoItems
                |> Seq.iter (fun item -> log.LogInformation item.Id)

                OkObjectResult("Hello")

我试过在function.json文件中指定绑定,但是无论在那里添加什么,bin / HttpTrigger / function.json中的文件仅包含HttpTrigger(C#和F#):

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-3.0.1",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "httpTrigger",
      "methods": [
        "get"
      ],
      "authLevel": "anonymous",
      "name": "req"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/Customer.dll",
  "entryPoint": "Customer.CosmosBindings.Run"
}

C#和F#的依赖项和函数版本相同:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.3" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.1" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="GetCustomer.fs" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

运行F#示例的错误:

[2/19/2020 4:01:52 PM] Starting JobHost
[2/19/2020 4:01:52 PM] Starting Host (HostId=havardandaestensenvippsnosmacboo, InstanceId=faf11b1c-b81d-49d2-8876-e4ef8785dc9f, Version=3.0.13107, ProcessId=30665, AppDomainId=1, InDebugMode=False, InDiagnosticMode=False, FunctionsExtensionVersion=(null))
[2/19/2020 4:01:52 PM] Loading functions metadata
[2/19/2020 4:01:52 PM] 1 functions loaded
[2/19/2020 4:01:52 PM] Generating 1 job function(s)
[2/19/2020 4:01:52 PM] Microsoft.Azure.WebJobs.Host: Error indexing method 'HttpTrigger'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'toDoItems' to type IEnumerable`1. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
[2/19/2020 4:01:52 PM] Error indexing method 'HttpTrigger'
[2/19/2020 4:01:52 PM] Microsoft.Azure.WebJobs.Host: Error indexing method 'HttpTrigger'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'toDoItems' to type IEnumerable`1. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
[2/19/2020 4:01:52 PM] Function 'HttpTrigger' failed indexing and will be disabled.
[2/19/2020 4:01:52 PM] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
[2/19/2020 4:01:52 PM] Initializing function HTTP routes
[2/19/2020 4:01:52 PM] Mapped function route 'api/HttpTrigger' [get] to 'HttpTrigger'
[2/19/2020 4:01:52 PM]
[2/19/2020 4:01:52 PM] Host initialized (199ms)
[2/19/2020 4:01:52 PM] Host started (206ms)
[2/19/2020 4:01:52 PM] Job host started
[2/19/2020 4:01:52 PM] The 'HttpTrigger' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'HttpTrigger'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'toDoItems' to type IEnumerable`1. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

从F#代码删除类型,只是返回toDoItemsCosmosDB也导致错误。

[2/19/2020 4:22:33 PM] Generating 1 job function(s)
[2/19/2020 4:22:33 PM] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
[2/19/2020 4:22:33 PM] Initializing function HTTP routes
霍华德·安达·埃斯坦森

事实证明,F#模板中存在一个用于生成函数的错误。还有的是一个开放的问题了近一年,你必须使用Include,而不是Updatehost.jsonlocal.settings.json这是正确的.fsproj文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.3" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.1" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="GetCustomer.fs" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </Content>
  </ItemGroup>
</Project>

我很困惑,为什么必须指定输出变量的返回类型。我相信不这样做是完全有效的F#。错误消息对调试没有帮助:

[3/3/2020 11:57:09 AM] An unhandled host error has occurred.
[3/3/2020 11:57:09 AM] Microsoft.Azure.WebJobs.Host: 'HttpTrigger' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?.

为了记录在案,toDoItems: ToDoItem seq并且toDoItems: IEnumerable<ToDoItem>两者产生相同的结果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Azure函数的Azure Cosmos DB输入绑定不起作用

来自分类Dev

是否可以从函数内部使用变量动态调用Azure函数的Cosmos DB输入绑定的SQL查询?

来自分类Dev

带Cosmos DB绑定的Azure函数返回格式化的DateTime吗?

来自分类Dev

Azure Function EventHub输出绑定

来自分类Dev

无法为Python 3.8 Azure Function加载azure-cosmos库

来自分类Dev

Requirejs出现Knockout错误:无法处理绑定“ component:function(){return f}”-不匹配的匿名define()模块

来自分类Dev

Azure Function App Cosmos DB触发器连接下降

来自分类Dev

在预编译的 C# Azure 函数中使用命令式绑定时,如何定义 Cosmos DB 数据库和集合名称?

来自分类Dev

Azure Cosmos DB查询

来自分类Dev

Azure Cosmos DB 分区

来自分类Dev

Azure Function 2.0 绑定属性的 ServiceBusTrigger

来自分类Dev

Requirejs出现敲除错误:无法处理绑定“ component:function(){return f}”-不匹配的匿名define()模块

来自分类Dev

Azure Cosmos DB相关子查询无法按预期工作

来自分类Dev

Azure Cosmos DB批量导入

来自分类Dev

批量插入Azure Cosmos DB

来自分类Dev

使用Java和Postgres DB绑定的Azure函数

来自分类Dev

无法使用Microsoft.EntityFrameworkCore.Cosmos连接到Azure Cosmos Db帐户-响应状态代码

来自分类Dev

Azure 函数错误:Microsoft.Azure.WebJobs.Host:索引方法“Function1”时出错。无法将参数“文档”绑定到类型 IAsyncCollector`

来自分类Dev

为什么在F#Azure Functions项目中未将host.json复制到输出?

来自分类Dev

Azure Function输出绑定是否具有重试策略?

来自分类Dev

如何在Python中将Azure Function绑定到Blob容器?

来自分类Dev

Azure自动化例外:无法绑定参数“ ScriptBlock”

来自分类Dev

Azure Cosmos DB Mongo-资源令牌

来自分类Dev

不支持Azure Cosmos DB查询

来自分类Dev

Azure Cosmos DB 中嵌套字段的索引

来自分类Dev

Azure 函数 - Cosmos DB 删除文档问题

来自分类Dev

在 azure Cosmos db 上创建集合太慢

来自分类Dev

Azure Cosmos DB sql join 和 udf

来自分类Dev

Azure Functions 模型绑定

Related 相关文章

  1. 1

    Azure函数的Azure Cosmos DB输入绑定不起作用

  2. 2

    是否可以从函数内部使用变量动态调用Azure函数的Cosmos DB输入绑定的SQL查询?

  3. 3

    带Cosmos DB绑定的Azure函数返回格式化的DateTime吗?

  4. 4

    Azure Function EventHub输出绑定

  5. 5

    无法为Python 3.8 Azure Function加载azure-cosmos库

  6. 6

    Requirejs出现Knockout错误:无法处理绑定“ component:function(){return f}”-不匹配的匿名define()模块

  7. 7

    Azure Function App Cosmos DB触发器连接下降

  8. 8

    在预编译的 C# Azure 函数中使用命令式绑定时,如何定义 Cosmos DB 数据库和集合名称?

  9. 9

    Azure Cosmos DB查询

  10. 10

    Azure Cosmos DB 分区

  11. 11

    Azure Function 2.0 绑定属性的 ServiceBusTrigger

  12. 12

    Requirejs出现敲除错误:无法处理绑定“ component:function(){return f}”-不匹配的匿名define()模块

  13. 13

    Azure Cosmos DB相关子查询无法按预期工作

  14. 14

    Azure Cosmos DB批量导入

  15. 15

    批量插入Azure Cosmos DB

  16. 16

    使用Java和Postgres DB绑定的Azure函数

  17. 17

    无法使用Microsoft.EntityFrameworkCore.Cosmos连接到Azure Cosmos Db帐户-响应状态代码

  18. 18

    Azure 函数错误:Microsoft.Azure.WebJobs.Host:索引方法“Function1”时出错。无法将参数“文档”绑定到类型 IAsyncCollector`

  19. 19

    为什么在F#Azure Functions项目中未将host.json复制到输出?

  20. 20

    Azure Function输出绑定是否具有重试策略?

  21. 21

    如何在Python中将Azure Function绑定到Blob容器?

  22. 22

    Azure自动化例外:无法绑定参数“ ScriptBlock”

  23. 23

    Azure Cosmos DB Mongo-资源令牌

  24. 24

    不支持Azure Cosmos DB查询

  25. 25

    Azure Cosmos DB 中嵌套字段的索引

  26. 26

    Azure 函数 - Cosmos DB 删除文档问题

  27. 27

    在 azure Cosmos db 上创建集合太慢

  28. 28

    Azure Cosmos DB sql join 和 udf

  29. 29

    Azure Functions 模型绑定

热门标签

归档