在 Azure ServiceBus 上从 .NET Core 发件人向 .NET 4.6 处理程序发送消息 [英] Send a message on Azure ServiceBus from .NET Core sender to a .NET 4.6 handler

查看:18
本文介绍了在 Azure ServiceBus 上从 .NET Core 发件人向 .NET 4.6 处理程序发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 .NET Core 控制台应用程序通过 Azure 服务总线发送消息,并在 .NET 4.6 控制台应用程序中接收它.在 .NET Core 中,我将 Azure 的新服务总线客户端用于尚未用于生产的发件人(根据他们的自述文件).

I want to send a message over Azure Service Bus from a .NET Core console app and receive it with in a .NET 4.6 console app. In .NET Core I'm using Azure's new service bus client for the sender which is not yet intended for production (according to their readme).

https://github.com/Azure/azure-service-bus-dotnet

我可以使用示例轻松地从 .NET Core 发送和使用 .NET Core 接收.但是,当 .NET 4.6 应用程序订阅主题并收到相同的消息时,.NET 4.6 应用程序会抛出此异常:

I can send from .NET Core and receive with .NET Core easily enough using the samples. However when the .NET 4.6 app subscribes to the Topic and receives the same message the .NET 4.6 app throws this exception:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: 
Exception while executing function: Functions.ProcessEvent
System.InvalidOperationException: Exception binding parameter 'message' 
The BrokeredMessage with ContentType 'string' failed to 
deserialize to a string with the message: 
'Expecting element 'string' from namespace 
'http://schemas.microsoft.com/2003/10/Serialization/'.. 
Encountered 'Element'  with name 'base64Binary', namespace 
http://schemas.microsoft.com/2003/10/Serialization/'. ' ---> 
System.Runtime.Serialization.SerializationException: Expecting element
'string' from namespace 
http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element'
with name 'base64Binary', namespace 
'http://schemas.microsoft.com/2003/10/Serialization/'. 


System.Runtime.Serialization.DataContractSerializer.InternalReadObject
(XmlReaderDelegator xmlReader, Boolean verifyObjectName, 
DataContractResolver dataContractResolver) at 
 System.Runtime.Serialization.XmlObjectSerializer.
ReadObjectHandleExceptions(XmlReaderDelegator reader, 
Boolean verifyObjectName, DataContractResolver dataContractResolver)

我的 .NET Core 发送方代码是:

My .NET Core sender code is:

using Microsoft.Azure.ServiceBus;

var topicClient = new TopicClient(ServiceBusConnectionString, "topic1");
var msg = new Message(Encoding.UTF8.GetBytes("Hello world"));
topicClient.SendAsync(msg).Wait();

我的 .NET 4.6 接收器代码是:

My .NET 4.6 receiver code is:

    using Microsoft.Azure.WebJobs;

    static void Main()
    {
        var config = new JobHostConfiguration();
        config.UseTimers();
        config.UseServiceBus();
        var host = new JobHost(config);
        host.RunAndBlock();
    }

    public void ProcessEvent([ServiceBusTrigger("topic1", "the-same-endpoint-as-connection-string")] string message, TextWriter logger)
    {
        Console.Writeline(message);
    }

请注意,我无法更改接收器,因为它是旧系统.

Note I can't change the receiver as it's a legacy system.

推荐答案

我想问题是因为 .NET Core 将序列化为 JSON 的消息发布到主题,但 NET 4.6 代码是尝试使用 DataContract Serializer(XML?),据我所知,这是完整的 .Net 框架库的默认反序列化方法.

I guess the problem is because the .NET Core is publishing the message serialized as JSON to the topic but the NET 4.6 code is trying to deserialize that message from the subscription with the DataContract Serializer (XML?), as far as I know that's the default deserialization method for the full .Net framework library.

如果是这种情况并且您无法修改接收代码,则需要在 .Net Core 上使用 DataContractSerializer 对消息进行序列化:

If that's the case and you cannot modify the receiving code, you will need to serialize the message with DataContractSerializer on .Net Core:

var ser = new System.Runtime.Serialization.DataContractSerializer(typeof(string));
var ms = new MemoryStream();
ser.WriteObject(ms, "hello world");
var msg = new Message(ms.ToArray());

你需要nuget包System.Runtime.Serialization.Xml

这篇关于在 Azure ServiceBus 上从 .NET Core 发件人向 .NET 4.6 处理程序发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆