Incorrect MassTransit message input

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.

+8
c # event-handling masstransit
source share
1 answer

Edit: Serializer doesn't matter here. I did not think so.

You can call Bus.Instance.Publish with information about the correct type by making a reflection in the Event object and getting its actual type. This will be some kind of awkward code, but as soon as possible it's easy to reuse. At Magnum, we have an extension method to help with this.

 Bus.Instance.FastInvoke(new[]{ event.GetType() }, "Publish", event); 

Join us on the http://groups.google.com/group/masstransit-discuss mailing list and we will be happy to discuss in more detail.

+10
source share

All Articles