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

我想从.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应用程序会抛出此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发送者代码是:

 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里程碑 。 您可以在GitHub仓库下跟踪它。