升级到 .Net 6 和 Fuctions 4 后,EventData JSON 反序列化错误
我正在将带有 Functions v3 的 .Net 3.1 升级到带有 Functions v4 的 .Net 6,并且我有这个 EventHubTrigger:
[EventHubTrigger("clean-station-kfi-v1", Connection = "EventHubCleanConnectionString", ConsumerGroup = "local-testing")] EventData[] eventHubMessages
升级后我收到此错误:
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: KfiPersister
---> System.InvalidOperationException: Exception binding parameter 'eventHubMessages'
---> System.InvalidOperationException: Binding parameters to complex objects (such as 'EventData') uses Json.NET serialization.
1. Bind the parameter type as 'string' instead of 'EventData' to get the raw values and avoid JSON deserialization, or
2. Change the queue payload to be valid json. The JSON parser failed: Unable to find a constructor to use for type Microsoft.Azure.EventHubs.EventData. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Id', line 1, position 6.
我正在使用 Microsoft.Azure.WebJobs.Extensions.EventHubs 版本 5.0.1
我有使用:
using Microsoft.Azure.EventHubs;
我确实找到了一些关于使用不同名称空间的信息,如下所示:
using Azure.Messaging.EventHubs;
这是正确的路径还是如何在这里解决此问题?不过,切换到该名称空间会导致许多其他问题:)
谢谢
索伦
I am upgrading a .Net 3.1 with Functions v3 to .Net 6 with Functions v4, and I have this EventHubTrigger:
[EventHubTrigger("clean-station-kfi-v1", Connection = "EventHubCleanConnectionString", ConsumerGroup = "local-testing")] EventData[] eventHubMessages
After the upgrade I get this error:
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: KfiPersister
---> System.InvalidOperationException: Exception binding parameter 'eventHubMessages'
---> System.InvalidOperationException: Binding parameters to complex objects (such as 'EventData') uses Json.NET serialization.
1. Bind the parameter type as 'string' instead of 'EventData' to get the raw values and avoid JSON deserialization, or
2. Change the queue payload to be valid json. The JSON parser failed: Unable to find a constructor to use for type Microsoft.Azure.EventHubs.EventData. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Id', line 1, position 6.
I am using Microsoft.Azure.WebJobs.Extensions.EventHubs version 5.0.1
I have this using:
using Microsoft.Azure.EventHubs;
I did find something about using a different namespace like this:
using Azure.Messaging.EventHubs;
Is this the right path or how do I fix this problem here? Switching to that namespace causes lots of other issues though :)
Thank you
Søren
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 v5.0 开始,
Microsoft.Azure.WebJobs.Extensions.EventHubs
包在内部移至Azure.Messaging.EventHubs
包。这意味着 Function 签名中公开的类型源自该包。由于您的应用程序包含
Microsoft.Azure.EventHubs
的 using 语句,因此签名中的类型将解释为:由于
EventData
类型属于旧包,因此触发器无法绑定到它。如果可以的话,我们建议更新您的函数以使用
Azure.Messaging.EventHubs
,因为Microsoft.Azure.EventHubs
库已被正式弃用。 (ref)如果您有现有投资在
Microsoft.Azure.EventHubs
中,这使得迁移包变得困难,最简单的选择是使用 v4.3.1 的Microsoft。 Azure.WebJobs.Extensions.EventHubs
pacakge,它在内部使用旧库。或者,您可以考虑在应用程序中同时使用
Azure.Messaging.EventHubs
和Microsoft.Auzre.EventHubs
,并在绑定中使用完整的命名空间来消除EventData< /代码>;该场景的挑战是,如果您直接在函数主体中使用事件中心客户端,则需要在旧版和当前
EventData
类型之间复制数据。绑定更新看起来像:
Starting with v5.0, the
Microsoft.Azure.WebJobs.Extensions.EventHubs
package moved to theAzure.Messaging.EventHubs
package internally. This means that the types exposed in the Function signature originate in that package.Since your application includes a using statement for
Microsoft.Azure.EventHubs
, the type in your signature is interpreted as:Because the
EventData
type belongs to the legacy package, the trigger cannot bind to it.If you're able, we recommend updating your Function to use
Azure.Messaging.EventHubs
, as theMicrosoft.Azure.EventHubs
library has been officially deprecated. (ref)If you have an existing investment in
Microsoft.Azure.EventHubs
that makes migrating packages difficult, the most straightforward option would be to use v4.3.1 of theMicrosoft.Azure.WebJobs.Extensions.EventHubs
pacakge, which uses the legacy library internally.Alternatively, you could consider using both
Azure.Messaging.EventHubs
andMicrosoft.Auzre.EventHubs
in your application and using the full namespace in your binding to disambiguateEventData
; the challenge for that scenario is if you're directly using the Event Hubs clients in your Function body, you would need to copy data between the legacy and currentEventData
types.The binding update would look something like: