升级到 .Net 6 和 Fuctions 4 后,EventData JSON 反序列化错误

发布于 2025-01-14 13:23:28 字数 1374 浏览 1 评论 0原文

我正在将带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2025-01-21 13:23:28

从 v5.0 开始,Microsoft.Azure.WebJobs.Extensions.EventHubs 包在内部移至 Azure.Messaging.EventHubs 包。这意味着 Function 签名中公开的类型源自该包。

由于您的应用程序包含 Microsoft.Azure.EventHubs 的 using 语句,因此签名中的类型将解释为:

[EventHubTrigger("clean-station-kfi-v1", 
    Connection = "EventHubCleanConnectionString", 
    ConsumerGroup = "local-testing")] 
    Microsoft.Azure.EventHubs.EventData[] eventHubMessages)

由于 EventData 类型属于旧包,因此触发器无法绑定到它。

如果可以的话,我们建议更新您的函数以使用 Azure.Messaging.EventHubs,因为 Microsoft.Azure.EventHubs 库已被正式弃用。 (ref)

如果您有现有投资在 Microsoft.Azure.EventHubs 中,这使得迁移包变得困难,最简单的选择是使用 v4.3.1Microsoft。 Azure.WebJobs.Extensions.EventHubs pacakge,它在内部使用旧库。

或者,您可以考虑在应用程序中同时使用 Azure.Messaging.EventHubsMicrosoft.Auzre.EventHubs,并在绑定中使用完整的命名空间来消除 EventData< /代码>;该场景的挑战是,如果您直接在函数主体中使用事件中心客户端,则需要在旧版和当前 EventData 类型之间复制数据。

绑定更新看起来像:

[EventHubTrigger("clean-station-kfi-v1", 
    Connection = "EventHubCleanConnectionString", 
    ConsumerGroup = "local-testing")] 
    Azure.Messaging.EventHubs.EventData[] events)

Starting with v5.0, the Microsoft.Azure.WebJobs.Extensions.EventHubs package moved to the Azure.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:

[EventHubTrigger("clean-station-kfi-v1", 
    Connection = "EventHubCleanConnectionString", 
    ConsumerGroup = "local-testing")] 
    Microsoft.Azure.EventHubs.EventData[] eventHubMessages)

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 the Microsoft.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 the Microsoft.Azure.WebJobs.Extensions.EventHubs pacakge, which uses the legacy library internally.

Alternatively, you could consider using both Azure.Messaging.EventHubs and Microsoft.Auzre.EventHubs in your application and using the full namespace in your binding to disambiguate EventData; 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 current EventData types.

The binding update would look something like:

[EventHubTrigger("clean-station-kfi-v1", 
    Connection = "EventHubCleanConnectionString", 
    ConsumerGroup = "local-testing")] 
    Azure.Messaging.EventHubs.EventData[] events)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文