繁体   English   中英

在Azure ServiceBus上将消息从.NET Core发送方发送到.NET 4.6处理程序

[英]Send a message on Azure ServiceBus from .NET Core sender to a .NET 4.6 handler

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

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

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

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发件人代码是:

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接收器代码是:

    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);
    }

注意我不能更改接收器,因为它是旧系统。

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

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

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

听起来像是互操作性问题

该修复程序已合并到dev分支中,但尚未发布。 预定为0.0.7-preview里程碑0.0.7-preview 您可以在GitHub仓库下跟踪它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM