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

て烟熏妆下的殇ゞ 提交于 2019-12-08 03:20:51

问题


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

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

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.


回答1:


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.

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

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




回答2:


Sounds like an interoperability issue

The fix was merged into dev branch, but hasn't gone out yet. Scheduled for 0.0.7-preview milestone`. You can track it under the GitHub repo.



来源:https://stackoverflow.com/questions/45067209/send-a-message-on-azure-servicebus-from-net-core-sender-to-a-net-4-6-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!