2017-09-05 100 views
0

创建使用Azure表存储作为输入绑定并尝试检索多个实体的Azure函数时而不是只是一个单一的enntity我得到以下错误:Microsoft.Azure.WebJobs.Host.Tables.TableExtension + TableToIQueryableConverter`1 [TElement]'违反了'TElement'类型的约束

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}"); 
} 

回答

3

我已经找到了答案以上我自己,但由于错误信息没有得到我一个答案很快我figur我会发布并自己回答这个问题。 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}"); 
} 
+0

他们可能有类似'IQueryable 其中TEntity:TableEntity'的代码,那么你会得到一个错误消息,因为你的实体类型没有满足约束条件。 – juunas

+0

你的意思是命名你的TableEntity类'Task'吗?这将与内置的System.Threading.Task冲突。 –

+1

@麦克斯这确实是一个糟糕的命名选择。它在实际代码中被重新命名,并且为了好的措施,我会在这里更改它以确保没有人意外复制它。 –

1

如这里所描述的错误是因为我用我的实体模型不从EntityTable派生引起的

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

但是,你只是在这里使用一个foreach,所以你可以做简单的绑定。 我们有一个工作项直接绑定到T [](其中T没有约束)。 https://github.com/Azure/azure-webjobs-sdk/issues/972。随意Upvote,如果它会对你的情况有用。