Microsoft.Azure.WebJobs.Host.Tables.TableExtension+TableToIQueryableConverter`1[TElement]' 违反类型约束'TElement'

Microsoft.Azure.WebJobs.Host.Tables.TableExtension+TableToIQueryableConverter`1[TElement]' violates the constraint of type 'TElement'

创建使用 Azure Table 存储作为输入绑定的 Azure 函数并尝试检索多个实体而不是单个实体时,出现以下错误:

Error:
Function ($ScheduleTrigger) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.myTimerTrigger'. Microsoft.Azure.WebJobs.Host:  GenericArguments[0], 'Submission#0+Task', on  Microsoft.Azure.WebJobs.Host.Tables.TableExtension+TableToIQueryableConverter`1[     TElement]' violates the constraint of type 'TElement'. mscorlib: GenericArguments[0], 'Submission#0+Task', on 'Microsoft.Azure.WebJobs.Host.Tables.TableExtension+TableToIQueryableConverter`1    [TElement]' violates the constraint of type parameter 'TElement'.    
Session Id: f4a00564b4864fb3a131557dd45924c7    

Timestamp: 2017-09-05T07:48:09.738Z

我用于 C# 计时器触发器的代码如下:

using System;

public class Task
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public DateTime Timestamp { get; set; }
    public string Name { get; set; }
}

public static async Task Run(TimerInfo myTimer, IQueryable<Task> inputTable, TraceWriter log)
{
    foreach (var task in inputTable) {
        log.Info($"Processing task '{task.Name}' at: {DateTime.Now}");
    }
    log.Info($"Timer trigger executed at: {DateTime.Now}");
}

我自己找到了上述问题的答案,但由于错误消息没有让我很快得到答案,所以我想我 post 自己回答这个问题。

错误是因为我用于我的实体的模型不是从 EntityTable 派生的,如下所述:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-table

只需将上面的代码示例更改为以下内容即可修复错误:

using System;

public class MyInput : TableEntity
{
    public string Name { get; set; }
}

public static async Task Run(TimerInfo myTimer, IQueryable<MyInput> inputTable, TraceWriter log)
{
    foreach (var item in inputTable) {
        log.Info($"Processing item '{item.Name}' at: {DateTime.Now}");
    }
    log.Info($"Timer trigger executed at: {DateTime.Now}");
}

对于 IQueryable 绑定,T 必须是 TableEntity。请注意,绑定到 IQueryable 会忽略其他绑定属性(分区键、行键、过滤器、take)。

但是,您在这里只是使用了一个 foreach,因此您可以进行更简单的绑定。 我们有一个工作项可以直接绑定到 T[](其中 T 没有约束)。 https://github.com/Azure/azure-webjobs-sdk/issues/972。如果它对您的情况有用,请随时投票。