Question

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

So my questions are:

  • What is the correct way to cast the property-value to JObject?
  • Why does the cast fail? That means, what are the difficulties the clr has here?
  • Can I get the raw, unserialized message-body in my consumer?

Thank you.

Was it helpful?

Solution

You cannot use JSON serialization if you are doing runtime typing on any message contracts. If you want to do this, you'll require the use of the binary serializer.

You cannot access the raw, unserialized message-body; if the message cannot be deserialized, then no user code is called.

Having any types marked internal will not allow us to deserialize the message. A constructor cannot be called, thus no object creation. I'm not sure the binary serializer will allow you to get around this limitation, not something I've tested.

If you have other questions, you're welcomed to join the mailing list as well, https://groups.google.com/forum/#!forum/masstransit-discuss.

OTHER TIPS

As one of the creators of MassTransit, if you're including

public object MyMessageProperty { get; set; }

In your message contract, you're doing it wrong. Leverage the strongly typed publishing features of the framework instead of doing your own dynamic dispatch on top of the dispatching already being done by the publish/subscribe system inside MT.

My above described problem probably arose just out of a misconception of my messaging system. But I found a nasty workaround to convert the 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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top