在 Azure Function 升级到 .NET5 后,此签名会引发以下异常。
我已经根据这里的文档实现了它 https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-grid-trigger?tabs=csharp%2Cbash
[Function(nameof(CustomerCreated))]
public async Task CustomerCreated([EventGridTrigger] EventGridEvent eventGridEvent, FunctionContext functionContext)
{
// My implementation
}
System.NotSupportedException: 'Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Azure.Messaging.EventGrid.EventGridEvent'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
如何从 EventGrid 接收事件?
最佳答案
执行此操作的新方法似乎是 Azure.Messaging
命名空间中的 CloudEvent
。
(附注是数据属性区分大小写,我不确定它们是否以前。)
public async Task CustomerCreated([EventGridTrigger] CloudEvent eventGridEvent, FunctionContext functionContext)
{
// [...]
}
更新:
这在部署到 Azure 并从使用 Azure 函数作为端点的真实事件网格接收事件时中断。但是在使用 ngrok 和 webhook 进行本地测试时工作正常。回到第一格。
在用过时的示例进行无休止的谷歌搜索后,我在 document 中找到了一条注释。 , 指向 this example表明您需要创建一个自定义类型(看起来就像 EventGridEvent)。
经过测试,我发现这确实有效:
传递 string
而不是 EventGridEvent
并使用 EventGridEvent
中的实用函数解析实际事件。
[Function(nameof(CustomerCreated))]
public async Task CustomerCreated([EventGridTrigger] string data, FunctionContext functionContext)
{
var eventGridEvent = EventGridEvent.Parse(BinaryData.FromString(data));
// Rest of code
}
另一个相关的警告是,您不能从门户创建函数,但需要从命令行运行它,例如:
az eventgrid event-subscription create --name customer-created \
--source-resource-id <event-grid-topic-resource-id> \
--endpoint <azure-func-resource-id> --endpoint-type azurefunction
(资源id:s可以在http://resources.azure.com/找到)
你好微软;
使用这种方法在 .NET5 上运行 Azure Functions 感觉不是很 GA。我真的希望能够通过官方库从其他 Azure 服务中传递 EventGridEvent 等类型化对象。
我真的认为 .NET5/dotnet-isolated 上的 EventGridEvent 应该与 Azure Functions 兼容,与以前的 .NET 版本和公共(public)示例中的级别相同。
关于c# - 使用 [EventGridTrigger] EventGridEvent 作为参数时,Azure Function .NET5(隔离)抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67918950/