Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom queue object not receiving in Steeltoe rabbitMQ messaging #232

Closed
ahmedanwar100 opened this issue Jan 8, 2022 · 11 comments
Closed
Assignees
Labels

Comments

@ahmedanwar100
Copy link
Contributor

Describe the bug

I simply follow this article https://docs.steeltoe.io/guides/messaging/rabbitmq.html?tabs=cli but instead of passing data as string, i create a custom object and pass it in queue. When i go to the MonitorRabbitMQ project and put a breakpoint on listener, it does not hit and i got some exceptions.

Steps to reproduce

Steps to reproduce the behavior:

  1. follow this article https://docs.steeltoe.io/guides/messaging/rabbitmq.html?tabs=cli
  2. Just create a Custom class say CustomQueue and pass it in queue e.g.
[Serializable]
    public class CustomQueue
    {
        public string Name { get; set; }
    }
  1. Now in MonitorRabbitMQ project, change the param type e.g.
[RabbitListener(RECEIVE_AND_CONVERT_QUEUE)]
      public void ListenForAMessage(CustomQueue msg)
      {
          _logger.LogInformation($"Received the message '{msg}' from the queue.");
      }
  1. In MonitorRabbitMQ project, i copy the CustomQueue from WriteToRabbitMQ and put in MonitorRabbitMQ project
  2. The problem is that it does not hit when we pass a custom object and when we pass string as param then it successfully hit

Expected behavior

It should receive custom object

Environment (please complete the following information):

  • Messaging
  • Windows
  • NET Core 3.1
  • Steeltoe.Messaging.RabbitMQ Version=3.1.2

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context or links

Got this exception

Steeltoe.Messaging.RabbitMQ.Listener.Exceptions.ListenerExecutionFailedException: Listener threw exception
 ---> Steeltoe.Messaging.Converter.MessageConversionException: failed to convert serialized Message content
 ---> System.Runtime.Serialization.SerializationException: Unable to find assembly 'WriteToRabbitMQ, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(BinaryParser serParser, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, Boolean check)
   at Steeltoe.Messaging.RabbitMQ.Support.Converter.SimpleMessageConverter.FromMessage(IMessage from, Type targetType, Object convertionsHint)
   --- End of inner exception stack trace ---
   at Steeltoe.Messaging.RabbitMQ.Support.Converter.SimpleMessageConverter.FromMessage(IMessage from, Type targetType, Object convertionsHint)
   at Steeltoe.Messaging.RabbitMQ.Listener.Adapters.MessagingMessageListenerAdapter.OnMessage(IMessage amqpMessage, IModel channel)
   at Steeltoe.Messaging.RabbitMQ.Listener.AbstractMessageListenerContainer.DoInvokeListener(IChannelAwareMessageListener listener, IModel channel, IMessage message)
   --- End of inner exception stack trace ---
@ahmedanwar100
Copy link
Contributor Author

i think we have to serialize the object like this
_rabbitTemplate.ConvertAndSend(RECEIVE_AND_CONVERT_QUEUE, JsonConvert.SerializeObject(customMsg));

and then make Listner like this

[RabbitListener(RECEIVE_AND_CONVERT_QUEUE)] public void ListenForAMessage(string msg) { var x=JsonConvert.DeserializeObject<CustomQueue>(msg); _logger.LogInformation($"Received the message '{msg}' from the queue."); }

@dtillman
Copy link
Contributor

@bradLucifer : Yes, the problem you are seeing is that by default we use BinaryFormatter to serialize objects. As a result on the receiving side the deserialization attempts to deserialize the payload and expects to find the type available so that it can create an instance of it. The type if finds is not the same one that is being sent because the assemblies they are in are different. So you have a couple options:

  1. Put the common type CustomQueue in a separate project and reference it the other two rabbitmq projects. That way the type will be common and in a shared assembly accessible by both.
  2. Switch to using Json for the serialization protocol.
    Dave

@ahmedanwar100
Copy link
Contributor Author

ahmedanwar100 commented Jan 18, 2022 via email

@dtillman
Copy link
Contributor

@bradLucifer : Which do you prefer .. 1. Common project/.NET Serialization 2. JSON?

@ahmedanwar100
Copy link
Contributor Author

ahmedanwar100 commented Jan 18, 2022 via email

@dtillman
Copy link
Contributor

@bradLucifer : https://github.com/SteeltoeOSS/Samples/tree/main/Messaging/src/RabbitMQWeb2

I think this should help... See the Startup.cs file in each project for how to enable/disable the .NET serialization vs JSON.

@ahmedanwar100
Copy link
Contributor Author

ahmedanwar100 commented Feb 7, 2022 via email

@dtillman
Copy link
Contributor

dtillman commented Feb 7, 2022

In the startup.cs files for both projects, see the following comments.:

// Add Steeltoe Rabbit services, use default .NET serialization
//services.AddRabbitServices();

// Add Steeltoe Rabbit services, use JSON serialization
services.AddRabbitServices(true);

@ahmedanwar100
Copy link
Contributor Author

ahmedanwar100 commented Feb 9, 2022 via email

@ahmedanwar100
Copy link
Contributor Author

ahmedanwar100 commented Feb 14, 2022 via email

@ahmedanwar100
Copy link
Contributor Author

ahmedanwar100 commented Feb 17, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants