2

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.

ddcc3432
  • 459
  • 1
  • 6
  • 16
  • Can you add the handler code for the console app? How are you attempting to read the message? – Jesse Carter Jul 12 '17 at 20:48
  • Looks like the issue might be with the .NET serializer, can you try to use JSON serialization? – Andy T Jul 12 '17 at 20:59
  • You send the queue raw bytes but expect to receive a string on the other side. Part of the message meta-information is the content type, I think it does not know what to do with the byte->string conversion because it does not know what kind of byte[] you sent. – Scott Chamberlain Jul 12 '17 at 21:14
  • I tried setting the ContentType to 'base64Binary' or a serializable object that I shared between the project, neither worked. – ddcc3432 Jul 12 '17 at 21:25

2 Answers2

3

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

thepirat000
  • 12,362
  • 4
  • 46
  • 72
  • The BrokeredMessage with ContentType 'null' 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/'. ' – ddcc3432 Jul 13 '17 at 00:27
  • I tried your sample. I think the .NET 4.6 receiver needs to know the ContentType to deserialize it. – ddcc3432 Jul 13 '17 at 00:28
  • https://stackoverflow.com/questions/47427361/azure-service-bus-read-messages-sent-by-net-core-2-with-brokeredmessage-getbo – lohiarahul Nov 22 '17 at 05:28
  • does this work with broker properties also set? I tried setting the same user properties in net core to no avail – Fred Johnson Apr 18 '18 at 09:01
0

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.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80