8

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.

4

1 に答える 1

10

編集:シリアライザーはここでは関係ありません。私はこれを熟考しませんでした。

オブジェクトにリフレクションを実行し、実際の型も取得することBus.Instance.Publishで、正しい型情報で呼び出すことができます。Eventこれは厄介なコードになるでしょうが、いったん完了すると再利用が容易になるでしょう。Magnum には、これを支援する拡張メソッドがあります。

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

http://groups.google.com/group/masstransit-discussのメーリング リストに参加してください。詳細について喜んで話し合います。

于 2011-08-29T23:32:40.117 に答える