title: Configure MassTransit to deserialize polymorphic properties
body: <p>So, we are sending a message composed of a complex domain type. Our consumers are not triggering because MassTransit cannot deserialize the message and delegate to the consumer.</p>
  
  <p>This scenario may be demonstrated via</p>
  
  <pre><code>// message interface
  public interface ITestMessage { TestBaseClass Data { get; set; } };
  
  // message implementation
  public class TestMessage : ITestMessage
  {
      public TestBaseClass Data { get; set; }
  }
  
  // abstract child
  public abstract class TestBaseClass { }
  
  // a concrete implementation of abstract child, cannot be deserialized 
  // by default serializer configuration
  public class TestConcreteClass : TestBaseClass { }
  
  // simple consumer, uses a reset-event to synchronize with calling
  // test method
  public class TestConsumer : IConsumer&lt;ITestMessage&gt;
  {
      private readonly Action action = null;
      public TestConsumer(Action action) { this.action = action; }
      public Task Consume(ConsumeContext&lt;ITestMessage&gt; context)
      {
          action();
          return context.CompleteTask;
      }
  }
  
  [TestMethod]
  public void Publish_WhenPolymorphicMessage_ConsumesMessage()
  {
      ManualResetEvent isConsumed = new ManualResetEvent(false);
      IBusControl bus = Bus.Factory.CreateUsingInMemory(c =&gt;
      {
          InMemoryTransportCache inMemoryTransportCache = 
              new InMemoryTransportCache(Environment.ProcessorCount);
          c.SetTransportProvider(inMemoryTransportCache);
          c.ReceiveEndpoint(
              "", 
              e =&gt; e.Consumer&lt;TestConsumer&gt;(
                  () =&gt; new TestConsumer(() =&gt; isConsumed.Set())));
      });
      bus.Start();
  
      ITestMessage message = new TestMessage 
      {
          // comment out assignment below, et voila, we pass :S
          Data = new TestConcreteClass { },
      };
  
      // attempt to publish message and wait for consumer
      bus.Publish&lt;ITestMessage&gt;(message);
  
      // simple timeout fails
      Assert.IsTrue(isConsumed.WaitOne(TimeSpan.FromSeconds(5)));
  }
  </code></pre>
  
  <p>Of course, we may successfully demonstrate that a message with polymorphic child can be serialized</p>
  
  <pre><code>[TestMethod]
  public void Serialize_WithPolymorphicChild_DeserializesCorrectly()
  {
      ITestMessage message = new TestMessage { Data = new TestConcreteClass { }, };
      JsonSerializerSettings settings = 
          new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, };
      string messageString = JsonConvert.SerializeObject(message, settings);
      ITestMessage messageDeserialized = (ITestMessage)(JsonConvert.DeserializeObject(
          messageString, 
          settings));
  
      Assert.IsNotNull(messageDeserialized);
      Assert.IsNotNull(messageDeserialized.Data);
      Assert.IsInstanceOfType(messageDeserialized.Data, typeof(TestConcreteClass));
      Assert.AreNotSame(message.Data, messageDeserialized.Data);
  }
  </code></pre>
  
  <p>I have attempted various configurations, to no avail.</p>
  
  <pre><code>IBusControl bus = Bus.Factory.CreateUsingInMemory(c =&gt;
  {
      InMemoryTransportCache inMemoryTransportCache = 
          new InMemoryTransportCache(Environment.ProcessorCount);
      c.SetTransportProvider(inMemoryTransportCache);
  
      // attempt to set and configure json serializer; zero effect
      c.ConfigureJsonDeserializer(
          s =&gt; 
          {
              s.TypeNameHandling = TypeNameHandling.All; 
              return s;
          });
      c.ConfigureJsonSerializer(
          s =&gt; 
          {
              s.TypeNameHandling = TypeNameHandling.All; 
              return s; 
          });
      c.UseJsonSerializer();
  
      c.ReceiveEndpoint(
          "", 
          e =&gt; e.Consumer&lt;TestConsumer&gt;(
              () =&gt; new TestConsumer(() =&gt; isConsumed.Set())));
  });
  </code></pre>
  
  <p>I am seeking <em>any</em> successful solution, eg adornment of message/domain classes with <code>KnownType</code> attributes, or bus configuration. Anything that leads to a successful pass of test method above.</p>