Complex message and de-serialization of Masstransit

I am posting in MT that has several properties typed for an object, since I don't know the type at compile time. When I receive a message from the consumer, I see that the properties typed for the object are populated with instances of Newtonsoft JObject instances. The JObject-Class is located in ILMerged Newtonsoft.Json-assembly in Masstransit.dll. The JObject-Class in this assembly is marked as internal. Whenever I try to pass a property value to a JObject provided by Nuget-Assembly Newtonsoft.Json, it fails.

So my questions are:

  • What is the correct way to assign a value to a JObject property?
  • Why doesn't the cast work? Does this mean what difficulties clr has here?
  • Can I get a raw, uncertified message body in my consumer?

Thanks.

+4
source share
3 answers

You cannot use JSON serialization if you perform typing during the execution of any message contracts. If you want to do this, you will need to use a binary serializer.

You cannot access the unprocessed, uncertified message body; if the message cannot be deserialized, then the user code is not called.

If any types marked as internal do not allow the message to be deserialized. The constructor cannot be called, so there is no object creation. I'm not sure that a binary serializer will allow you to get around this limitation, and not what I tested.

If you have other questions, you can also join the mailing list, https://groups.google.com/forum/#!forum/masstransit-discuss .

+5
source

As one of the creators of MassTransit, if you turn on

public object MyMessageProperty { get; set; } 

You are doing it wrong in your messaging contract. Use the strongly typed structure publishing functions instead of performing your own dynamic dispatch over the dispatch already performed by the publish / sign system inside MT.

+2
source

My problem above was probably due to a misconception of my messaging system. But I found a nasty workaround for converting nested JObjects to the right domain objects:

 protected bool TryConvertJObjectToDtoOfType<T>(Object jObjectInDisguise, out T dto) where T: BpnDto { try { if (jObjectInDisguise.GetType().Name != typeof(JObject).Name) throw new ArgumentException("Object isn't a JObject", "jObjectInDisguise"); var json = jObjectInDisguise.ToString(); var settings = new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Error }; dto = JsonConvert.DeserializeObject<T>(json, settings); return true; } catch { dto = null; return false; } } 
0
source

Source: https://habr.com/ru/post/1394968/


All Articles