C# 在Azure ServiceBus上从.NET核心发件人向.NET 4.6处理程序发送消息
我想通过Azure服务总线从.NET核心控制台应用程序发送消息,并在.NET 4.6控制台应用程序中接收消息。在.NETCore中,我正在为发送方使用Azure的新服务总线客户端,该客户端尚未用于生产(根据其自述) 我可以从.NET Core发送和使用.NET Core接收样本,非常简单。但是,当.NET 4.6应用程序订阅主题并收到相同消息时,.NET 4.6应用程序会引发此异常:C# 在Azure ServiceBus上从.NET核心发件人向.NET 4.6处理程序发送消息,c#,.net,azure,.net-core,azureservicebus,C#,.net,Azure,.net Core,Azureservicebus,我想通过Azure服务总线从.NET核心控制台应用程序发送消息,并在.NET 4.6控制台应用程序中接收消息。在.NETCore中,我正在为发送方使用Azure的新服务总线客户端,该客户端尚未用于生产(根据其自述) 我可以从.NET Core发送和使用.NET Core接收样本,非常简单。但是,当.NET 4.6应用程序订阅主题并收到相同消息时,.NET 4.6应用程序会引发此异常: Microsoft.Azure.WebJobs.Host.FunctionInvocationExceptio
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核心发件人代码是:
using Microsoft.Azure.ServiceBus;
var topicClient = new TopicClient(ServiceBusConnectionString, "topic1");
var msg = new Message(Encoding.UTF8.GetBytes("Hello world"));
topicClient.SendAsync(msg).Wait();
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 4.6接收器代码为:
using Microsoft.Azure.ServiceBus;
var topicClient = new TopicClient(ServiceBusConnectionString, "topic1");
var msg = new Message(Encoding.UTF8.GetBytes("Hello world"));
topicClient.SendAsync(msg).Wait();
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);
}
注意:我无法更改接收器,因为它是一个遗留系统。听起来像是一个
修复程序已合并到
dev
分支中,但尚未退出。预定的。您可以在GitHub repo下跟踪它。我猜问题是因为.NET核心将序列化为JSON的消息发布到主题,但NET 4.6代码正试图用(XML?)反序列化订阅中的消息,据我所知,这是完整的.Net framework库的默认反序列化方法
如果是这种情况,并且您无法修改接收代码,则需要在.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
您可以为console应用程序添加处理程序代码吗?您试图如何读取消息?看起来问题可能出在.NET序列化程序上,您可以尝试使用JSON序列化吗?您发送队列原始字节,但希望在另一端接收字符串。消息元信息的一部分是内容类型,我认为它不知道如何处理字节->字符串转换,因为它不知道您发送的字节[]的类型。我尝试将ContentType设置为“base64Binary”或项目之间共享的可序列化对象,两者都不起作用。ContentType为“null”的BrokeredMessage未能反序列化为消息为“应为命名空间“”中的元素”字符串”的字符串。。遇到名称为“base64Binary”、命名空间为“”的“元素”。“我试过你的样品。我认为.NET4.6接收者需要知道ContentType才能对其进行反序列化。这是否也适用于设置了代理属性的情况?我尝试在NetCore中设置相同的用户属性,但没有成功