由于绑定错误,Azure Webjob 无法启动

问题描述

我有一个 webjob,它应该从 Azure 队列中读取数据并对找到的有效负载执行一些操作。我遇到的问题是 webjob 无法启动,这是错误

[02/17/2021 15:40:11 > e80592: INFO] [15:40:10 ERR] 错误索引方法“Functions.ProcessstudentPerfQueueMessages” [02/17/2021 15:40:11 > e80592: INFO] Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexingException:错误索引方法“Functions.ProcessstudentPerfQueueMessages” [02/17/2021 15:40:11 > e80592: INFO] ---> system.invalidOperationException:无法将参数“message”绑定到“StudentMessage”类型。 [02/17/2021 15:40:11 > e80592: INFO] 在 Microsoft.Azure.WebJobs.Host.Bindings.Data.ClassDataBindingProvider`1.TryCreateAsync(BindingProviderContext context) 在 C:\projects\azure-webjobs-sdk- rqm4t\src\Microsoft.Azure.WebJobs.Host\Bindings\Data\ClassDataBindingProvider.cs:line 35 [02/17/2021 15:40:11 > e80592: INFO] 在 Microsoft.Azure.WebJobs.Host.Bindings.Data.DataBindingProvider.TryCreateAsync(BindingProviderContext context) 在 C:\projects\azure-webjobs-sdk-rqm4t\ src\Microsoft.Azure.WebJobs.Host\Bindings\Data\DataBindingProvider.cs:line 41 [02/17/2021 15:40:11 > e80592: INFO] 在 Microsoft.Azure.WebJobs.Host.Bindings.CompositeBindingProvider.TryCreateAsync(BindingProviderContext context) 在 C:\projects\azure-webjobs-sdk-rqm4t\src\ Microsoft.Azure.WebJobs.Host\Bindings\BindingProviders\CompositeBindingProvider.cs:line 23 [02/17/2021 15:40:11 > e80592: INFO] 在 Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexer.IndexMethodAsyncCore(MethodInfo method,IFunctionIndexCollector index,CancellationToken cancelationToken) in C:\projects\azure-webjobs- sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Indexers\FunctionIndexer.cs:line 196 [02/17/2021 15:40:11 > e80592: INFO] 在 Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexer.IndexMethodAsync(MethodInfo method,CancellationToken cancelationToken) in C:\projects\azure-webjobs- sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Indexers\FunctionIndexer.cs:line 149 [02/17/2021 15:40:11 > e80592: INFO] ---内部异常堆栈跟踪结束--- [02/17/2021 15:40:11 > e80592: INFO] 在 Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexer.IndexMethodAsync(MethodInfo method,CancellationToken cancelationToken) in C:\projects\azure-webjobs- sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Indexers\FunctionIndexer.cs:line 157 [02/17/2021 15:40:11 > e80592: INFO] 在 Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexer.IndexTypeAsync(Type type,CancellationToken cancelationToken) in C:\projects\azure-webjobs- sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Indexers\FunctionIndexer.cs:line 85 [02/17/2021 15:40:13 > e80592: INFO] [15:40:13 FTL] Web 作业启动失败

这是代码

public class Functions
{
    private readonly IService _service;
    public Functions(IService service)
    {
         _service = service;
    }
    public async Task ProcessstudentPerfQueueMessages([QueueTrigger("studentperf")] StudentMessage message)
    {
         await new StudentPerfQueueProcessor(_pascoService).ProcessMessage(message));
     }

目前队列中没有任何消息。 如果我将 StudentMessage 更改为 string,则 webjob 在 Azure 中成功启动。这是一个网络核心网络作业。请帮忙。

解决方法

正如 Azure 函数的 document 所述:

使用方法参数(如字符串)访问消息数据 参数名称。您可以绑定到以下任何类型:

  • Object - Functions 运行时将 JSON 有效负载反序列化为 代码中定义的任意类的实例。

  • 字符串

  • 字节[]

  • CloudQueueMessage

不确定您如何定义 StudentMessage 类,但如果您可以使用 string 类型满足您的需求,您可以考虑使用它。

,

这里是学生班

public class StudentMessage
{
   public int StudentId { get; set; }
   public int SubjectId { get; set; }
   public StudentForm Form { get; set; }
   public Dictionary<int,AnswerStatus> QuestionAnswerStatus { get; set; }
   public string Message { get; set; }
   public DateTime Date { get; set; }
}

Doris Lv 让我想到了 StudentMessage 类。我决定在 StudentMessage 和 bam 中注释掉 Message 属性!它成功启动。必须与 Newtonsoft.json 相关。