saga - How can I correlate events in a masstransit state machine without using a Guid? -
i have defined following state machine in masstransit:
public class orderstatemachine : masstransitstatemachine<orderstate> { public orderstatemachine() { instancestate(x => x.status); event(() => ordercreated, x => x.correlateby(order => order.ordercode, ctx => ctx.message.ordercode).selectid(ctx => newid.nextguid())); //how should select id these events? event(() => orderprovisioned, x => x.correlateby(order => order.ordercode, ctx => ctx.message.ordercode)); event(() => orderinvoiced, x => x.correlateby(order => order.ordercode, ctx => ctx.message.ordercode)); state(() => created); state(() => finished); compositeevent(() => orderfinished, order => order.compositestatus, orderprovisioned, orderinvoiced); initially( when(ordercreated) .then(context => console.writeline("order created")) .transitionto(created)); during(created, when(orderfinished) .then(context => console.writeline("order finished")) .transitionto(finished) .finalize()); } public state created { get; set; } public state finished { get; set; } public event<ordercreated> ordercreated { get; set; } public event<orderprovisioned> orderprovisioned { get; set; } public event<orderinvoiced> orderinvoiced { get; set; } public event orderfinished { get; set; } } public class orderstate : sagastatemachineinstance { public guid correlationid { get; set; } public string ordercode { get; set; } public string status { get; set; } public compositeeventstatus compositestatus { get; set; } } public class ordercreated { public string ordercode { get; set; } public ordercreated(string ordercode) { ordercode = ordercode; } } public class orderinvoiced { public string ordercode { get; set; } public orderinvoiced(string ordercode) { ordercode = ordercode; } } public class orderprovisioned { public string ordercode { get; set; } public orderprovisioned(string ordercode) { ordercode = ordercode; } }
how can correlate orderprovisoned , orderinvoiced event same orderstate instance initial ordercreated event without sending guids in events , use ordercode property correlate them? if run example, never orderfinished event if both orderprovisioned , orderinvoiced sent, if add guids events , correlate them based on guid executed correctly.
i solved it, apparently must explicitly set custom correlationid on statemachine instance, expect masstransit me. adapted code initial state:
initially( when(ordercreated) .then(context => context.instance.ordercode = context.data.ordercode) .then(context => console.writeline("order created")) .transitionto(created));
also using saga factory works, think drop selectid logic saga factory overrules this, exception.
event(() => ordercreated, x => { x.correlateby(order => order.ordercode, ctx => ctx.message.ordercode); x.insertoninitial = true; x.setsagafactory(context => new orderstate { correlationid = newid.nextguid(), ordercode = context.message.ordercode }); x.selectid(context => newid.nextguid()); });
Comments
Post a Comment