c# - Azure Function v2 中的 BrokeredMessage 发送和消息使用者

标签 c# azure azure-functions azureservicebus

我有模拟的 azure web 作业,它会定期推送 BrokeredMessage到服务总线主题,就像这样

public void Simulate(
            [TimerTrigger("0 */30 * * * *", RunOnStartup = true)]
            TimerInfo timerInfo,
            [ServiceBus("%topic%")]
            out BrokeredMessage message)
        {
            message = new BrokeredMessage(
                new AwesomeContract()
                {

                });
        }

在azure function V2中,我尝试使用 Message 来使用它类。

public static void Integrate(
            [ServiceBusTrigger(
                "%topic%",
                "%subscribtion%",
                Connection = "ServiceBusConnection")] Message message,
            TraceWriter log,
            ExecutionContext context)
        {
            try
            {
              message.GetBody<AwesomeContract>();
            }
        }

关于GetBody<>我收到DataContractSerialization异常“反序列化类型 的对象时出错。输入源的格式不正确。

BrokeredMessageMessage在 azure 函数 v1 和 v2 中兼容吗?有什么建议吗?

最佳答案

BrokeredMessage 是来自 WindowsAzure.ServiceBus 库的消息对象,其中 Message 是来自 Microsoft.Azure.ServiceBus 库的对象。

尽管这些库之间的对象类型不同,但将消息正文作为 Stream 发送将有助于克服读取正文时的异常。

message = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes([message_body]));

按照上面的方式构造消息会有所帮助。

关于c# - Azure Function v2 中的 BrokeredMessage 发送和消息使用者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52419414/

相关文章:

c# - 自定义操作 - 错误 1001 : Could not find file myApp. InstallState

c# - ExecuteNonQuery 结果为 "Connection property has not been initialized."

c# - 从另一台设备访问 Azure 模拟器

azure - 如何在 azure blob 存储中设置 `Content-Disposition : attachment` header ?

azure - TimerTrigger 在 Azure Functions 中并不总是触发

c# - 在公共(public)异常处理程序中的 catch block 外部抛出不带参数的语句

c# - 检索移动连接上的 Tcp/IP 使用情况 c#

azure - Microsoft 认知 - 人脸 API - 人脸验证 - 工作原理

azure - 如何将输入参数传递给 Start-NewOrchestration powershell 命令 let

azure - 将 Azure Functions 部署到多个函数中的特定函数而不覆盖其他函数