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

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

问题描述

我想通过Azure Service Bus从.NET Core控制台应用程序发送一条消息,并在.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)

My。 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序列化程序(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

You'll need the nuget package System.Runtime.Serialization.Xml

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

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