I have a problem with typical typing with messages that I am trying 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 try 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 from this collection, regardless of their specific types. If I pass the event to a specific type before publishing on the bus, I get the message correctly.
Any ideas on how I can fix this to allow the processing of an abstract collection of events without casting each one?
EDIT: I tried changing my settings to use BinarySerialization as follows:
Bus.Initialize(sbc => { sbc.UseBinarySerializer(); });
and did not give any changes in behavior. The only way I was able to get the Consumes<PersonCreated> class that needs to be called is to explicitly pass the event to the PersonCreated type.
c # event-handling masstransit
Jeff fritz
source share