Let us say that I have the following structure:
public class Parent
{
public string Id;
}
public class FirstChild:Parent
{
public string FirstName;
}
public class SecondChild:Parent
{
public string LastName;
}
And I have two services contacting via Request/Response messaging method.
My First service sends a message and expects either FirstChild or SecondChild as a response, so what I did is:
var client = bus.CreateRequestClient<CommandType, Parent>(serviceAddress, TimeSpan.FromSeconds(50));
var response = await client.Request(command);
The issue is that response is sent from second service containing Id and FirstName/LastName but received by first service containing only Id.
How can I override this issue?
Compose your classes rather than inherit them.
In general favour composition over inheritance Prefer composition over inheritance?
It will, for the most part, result in more flexible and easier to maintain code.
So rather than inherit parent, compose with it like this:
public class FirstChild
{
Parent parent {get; set;}
string firstName {get; set;}
}
In this way you are stating that FirstChild 'has-a' parent rather than FirstChild 'is-a' parent.
I believe this is fundamentally where your problem lies.
MassTransit documentation rightly cautions against using inheritance in messages. Don't confuse your message design with OO design. A message is only state without behavior.