I am running into a base-typing problem with messages I am attempting to publish through MassTransit. Consider the following:
[Serializable]
public abstract class Event : CorrelatedBy<Guid> {
public Guid CorrelationId { get; set; }
public abstract string EventName { get; }
public override string ToString() {
return string.Format("{0} - {1}", EventName, CorrelationId);
}
}
[Serializable]
public class PersonCreated : Event {
public PersonCreated(Guid personId, string firstName, string lastName) {
PersonId = personId;
FirstName = firstName;
LastName = lastName;
}
public readonly Guid PersonId;
public readonly string FirstName;
public readonly string LastName;
}
However, when I attempt to publish a collection of abstract events with something like:
public void PublishEvents(IEnumerable<Event> events) {
foreach (var e in events) {
Bus.Instance.Publish(e);
}
}
I do NOT receive any events out of this collection, regardless of their concrete types. If I cast the event to its proper concrete type before publishing on the bus, I do receive the message properly.
Any ideas as to how I can correct this to allow my abstract collection of Events to be processed without casting each one?
EDIT: I have attempted to change my settings to use BinarySerialization like so:
Bus.Initialize(sbc =>
{
sbc.UseBinarySerializer();
});
and have not yielded any change in behavior. The only way that I have been able to get my Consumes<PersonCreated>
class to be called is to explicitly cast an event to PersonCreated
type.